I'm new with remote messages in android and I executing this code when receive a message from server. The idea is start the app when the message arrive to app, it's working ok till sdk 26, but in the new sdk versions the message don't start the app. What extra I need ? some permissions, other flag ?
public void onMessageReceived(RemoteMessage message) {
try {
Uri uriBeep = Uri.parse("someExample" + R.raw.win);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_HIGH);
channel.enableLights(true);
channel.setLightColor(Color.GRAY);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
channel.setSound(uriBeep, audioAttributes);
channel.enableVibration(true);
if (manager != null) {
manager.createNotificationChannel(channel);
}
}
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setSound(uriBeep)
.setContentTitle(getString(R.string.app_name))
.setContentText(message.getData().get("message"))
.setAutoCancel(true)
.setPriority(1)
.setChannelId(NOTIFICATION_CHANNEL_ID)
.setVibrate(VIBRATE_PATTERN)
.setContentIntent(pendingIntent);
if (manager != null) {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
manager.notify(0, builder.build());
}
} catch (Exception e) {
e.printStackTrace();
}
}
Starting with Android 10 (API level 29) there are restrictions that prevent background code from starting activities. See https://developer.android.com/guide/components/activities/background-starts
The idea is that push notifications and other asynchronous events should not disturb the user from what he is currently doing. The acceptable approach is to show a
Notification
and the user can then open the app when he wants to. This puts control into the user's hand.