当应用程序未运行时,在Android Oreo中没有收到通知



我正在开发一个应用程序,当该应用程序未在Android Oreo中运行时,我不会收到通知。收到推送通知时,将调用 onMessageReceived 方法,但通知栏中不会显示通知。而如果应用程序正在运行(前台或后台(,我会在通知栏中收到通知。 public class MyFirebaseMessagingService 扩展 FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
RemoteMessage.Notification notification = remoteMessage.getNotification();
Intent intent = new Intent(this, ApplyAndInitiateMainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "100";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this,channelId)
.setLargeIcon(bitmap)
.setSmallIcon(R.image_new)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setWhen(remoteMessage.getSentTime())
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_HIGH)
.addAction(0,"Apply LEAVE",pendingIntent)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap).setSummaryText(message).bigLargeIcon(null));
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Channel human readable title", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build());
}

}

在创建通知之前创建通知渠道:

String channelId = "100";
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Channel human readable title", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setLargeIcon(bitmap)
.setSmallIcon(R.image_new)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setWhen(remoteMessage.getSentTime())
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_HIGH)
.addAction(0,"Apply LEAVE",pendingIntent)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap).setSummaryText(message).bigLargeIcon(null));

最新更新