After updating the app to 8.1 the notification was not shown and I fixed it. Now, pending intent is not working as expected.
After receiving the notification, I am not able to navigate to the app if it is in background and if it is closed it is not launching.
private void sendNotify(String messageBody) {
Intent intent = new Intent();
intent.setAction(Constants.NOTIFY);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
int uniqueId = (int) System.currentTimeMillis();
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Creates the PendingIntent
PendingIntent notifyPendingIntent =
PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
String channelID = "com.myapp.ind.push.ServiceListener";// The id of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(channelID, "MyApp", importance);
// Create a notification and set the notification channel.
Notification notification = getNotificationBuilder(messageBody, notifyPendingIntent, defaultSoundUri)
.setChannelId(channelID)
.build();
if (notificationManager != null) {
notificationManager.createNotificationChannel(mChannel);
notificationManager.notify(uniqueId, notification);
}
} else if (notificationManager != null) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
uniqueId /* Request code */,
intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = getNotificationBuilder(messageBody, pendingIntent, defaultSoundUri);
notificationManager.notify(uniqueId /* ID of notification */,
notificationBuilder.build());
}
}
private NotificationCompat.Builder getNotificationBuilder(String messageBody, PendingIntent pendingIntent, Uri defaultSoundUri) {
return new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle(getString(R.string.notification_title))
.setContentText(messageBody)
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
}
Edit:
On click of notification it was working fine till 6.0. After updating to 8.0 it is not working in Google Pixel devices. It is not opening the app or bringing the app to foreground.
setAction(...pendingIntent);
to notificationBuilder – WatersickIntent
to an explicitIntent
; i.e., one that targets your specific Receiver class - e.g.,Intent intent = new Intent(this, YourReceiver.class);
. Also, you havePendingIntent.getActivity()
instead ofPendingIntent.getBroadcast()
in theif
block. Mistype? – Polycythemia