gpt4 book ai didi

android - 托盘图标上未显示 Firebase 通知消息?

转载 作者:行者123 更新时间:2023-11-29 14:12:59 26 4
gpt4 key购买 nike

我正在使用 this example将 Firebase Cloud Messaging 与我的应用程序集成。通过 Firebase 控制台发送包含目标用户段主题(主题是新闻)的消息。我的应用程序将显示包含消息的 toast 。

我想要的是通知消息,它会自动显示在 Android 通知托盘中,而不是 toast。看完FCM concept ,我用 curl 试了一下:

curl -H "Content-type: application/json" -H "Authorization:key=AAAA....ahjkdkhajksd"  -X POST -d '{ "to": "/topics/news", "notification": { "body":"foo", "title":"bar", "icon":""}}' https://fcm.googleapis.com/fcm/send

它通过 Toast 显示,而不是 Android 通知托盘。这里可能出了什么问题?

最佳答案

如果您的应用程序在前台,通知不会自动显示到通知栏。如果您希望您的应用即使在前台也能显示通知,您需要添加以下代码。

MyFirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
JSONObject object = new JSONObject(remoteMessage.getData());
try {
String title = object.getString("title");
String message = object.getString("message");

Intent intent = new Intent(getBaseContext(), MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder b = new NotificationCompat.Builder(getBaseContext());

b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Ticker")
.setContentTitle(title)
.setContentText(message)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.setContentIntent(contentIntent)
.setContentInfo("Info");

Random r = new Random();
int randomNo = r.nextInt(100000000 + 1);

NotificationManager notificationManager = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(randomNo, b.build());
} catch (JSONException e) {
e.printStackTrace();
}
super.onMessageReceived(remoteMessage);
}
}

关于android - 托盘图标上未显示 Firebase 通知消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45319383/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com