应用未运行时未在Android Oreo中收到通知

时间:2018-09-24 09:22:57

标签: android push-notification android-notifications android-8.0-oreo

我正在开发一个应用程序,当该应用程序未在Android Oreo中运行时,我没有收到通知。接收到推送通知时,将调用onMessageReceived方法,但通知不会显示在通知栏中。而如果应用程序正在运行(前景或后台),我会在通知栏中收到通知。公共类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());
    }

}

1 个答案:

答案 0 :(得分:0)

在创建通知之前创建您的通知频道:

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));