【发布时间】: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