代码之家  ›  专栏  ›  技术社区  ›  Dean Blakely

通知管理器已停止工作

  •  0
  • Dean Blakely  · 技术社区  · 7 年前

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

    我刚把我的短信应用程序转换成FCM。你可以看到我经历的过程 here . 既然完成了,我的通知就不起作用了。如果我的FirebaseMessagingService收到一条消息,而主应用程序不活动,我会在正在运行的手机上创建一个通知。

    这是从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());
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Dean Blakely    7 年前

    这个问题解决了。一旦你为Android编写了一个应用程序,Google会让你在余生中全职工作,只是为了让它继续工作。 他们正在打破变革的恶魔

    实际上,这个代码在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());
        }