【问题标题】:Notification manager has stopped working通知管理器已停止工作
【发布时间】:2018-12-08 02:20:53
【问题描述】:

这个问题已经解决了。请参阅下面的解决方案。

我刚刚将我的消息传递应用程序转换为 FCM。你可以看到我经历过的过程here。现在它完成了,我的通知不再起作用。如果我的 FirebaseMessagingService 收到一条消息并且主应用程序未处于活动状态,我会在我正在运行的手机上创建一个通知。

这已在 GCM 上正确运行多年。当我跟踪代码时,一切正常 - 只是托盘中没有显示任何通知。我无法想象 Firebase 与此有什么关系。

这是从 FirebaseMessagingService 调用的代码。这段代码已经运行了好几年了。 . .

public static void raiseNotification( String username, String mesText, int count)
{
    String message = "From: " + username + " " + mesText;
    if (count > 1)
    {
        count--;
        message = message +  " + " + count + " more";
    }



    NotificationCompat.Builder b = new NotificationCompat.Builder(GlobalStuff.GCT);

    Intent intent = new Intent(GlobalStuff.GCT, MainActivity.class);
    intent.putExtra("whattodo", username);
    intent.setAction(Long.toString(System.currentTimeMillis())); //just to make it unique from the next one
    PendingIntent pIntent = PendingIntent.getActivity(GlobalStuff.GCT, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);


    b.setContentTitle("New SafeTalk Message")
            .setSmallIcon(R.drawable.ticon)
            .setContentText(message)
            .setTicker("New SafeTalk Message")
            .setContentIntent(pIntent)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setAutoCancel(true);
    //.addAction(R.drawable.smallredball, "Read Now", pIntent)
    //.addAction(R.drawable.smallquestion, "hello there", pIntent);

    NotificationManager mgr = (NotificationManager)GlobalStuff.GCT.getSystemService(NOTIFICATION_SERVICE);
    mgr.notify(0, b.build());
}

【问题讨论】:

  • 您不能只是将 FCM 换成 GCM 并期望它能够工作。要迁移到 FCM,您需要做很多事情……您的 onMessageReceived 方法在哪里?您的示例代码不足以识别您的问题
  • stackoverflow.com/questions/51031948/… 几乎解释了我所做的事情。这确实是一项相当多的工作。
  • 那么raiseNotification在哪里被调用?
  • 来自我对 FirebaseMessagingService 的扩展。用于从意图服务调用。我已经追查过了,mgr.notify 正在被执行。

标签: android firebase notifications firebase-cloud-messaging


【解决方案1】:

这个问题解决了。一旦你为 Android 编写了一个应用程序,谷歌就会让你在余生中全职工作,只是为了让它继续工作。 他们正在打破变革的恶魔。事实证明,这个通知问题与 Firebase 无关(它本身就是所有重大变化的根源)。

Google 更改了有关如何在 Oreo 中发送通知的要求。 Google 设计了此更改,以便如果您的应用程序在 Oreo 上运行并且您没有进行更改,您的通知将根本不起作用 - 希望没有人构建重要的通知。在 Oreo 中,他们需要一个 channelId。

这是适用于 Oreo 的代码。 . .

实际上这段代码在 Oreo 中并不完全有效。请参阅我关于奥利奥通知的下一篇文章

   private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String channelId = getString(R.string.default_notification_channel_id);
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.ic_stat_ic_notification)
                .setContentTitle("FCM Message")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        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 /* ID of notification */, notificationBuilder.build());
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    相关资源
    最近更新 更多