【问题标题】:Not getting notification in Android Oreo when the app is not running应用未运行时在 Android Oreo 中未收到通知
【发布时间】:2019-02-27 18:42:26
【问题描述】:

我正在开发一个应用程序,当该应用程序未在 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());
    }

}

【问题讨论】:

  • 您必须将您的唯一 ID 作为 notify() 方法中的第一个参数发送,例如 notificationManager.notify(channelId,notificationBuilder.build());
  • @AbhishekSharma 它没有将 channelId 作为第一个参数,因为 channelID 是字符串,并且该位置需要 int。
  • 好的,我以为那是您的唯一 ID。您必须生成一个唯一 id 作为第一个参数传递
  • 您可以像这样创建唯一 id:- uniqueId = (int) System.currentTimeMillis();

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


【解决方案1】:

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

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

【讨论】:

  • 我试过这个,但现在我也面临同样的问题。
  • 您是否检查过您的onMessageReceived 是否被调用,或者问题只是通知没有收到通知?因为看起来你的代码没问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-17
  • 1970-01-01
相关资源
最近更新 更多