【发布时间】:2018-02-09 08:10:45
【问题描述】:
当应用位于前台时,我正在尝试使用 firebase 显示通知。当我从服务器推送通知时调用了 onMessageReceived 方法,但通知不显示。
这是我的代码:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
Timber.d("FCM-From: " + remoteMessage.getFrom());
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
if (remoteMessage.getNotification() != null) {
Timber.d("FCM-Message Notification Body: " + remoteMessage.getNotification().getBody());
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getApplicationContext(), "CHANNEL_NOTIF")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("test")
.setContentText("test content");
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (manager != null) {
Timber.d("FCM-Notif");
manager.notify(1, builder.build());
}
}
}
});
}
}
在我的 logcat 中我可以看到:
FCM-消息通知正文:202018105166
FCM-发件人:1049809400953
FCM 通知
我关注了https://developer.android.com/training/notify-user/build-notification.html#builder
我在奥利奥上跑步
解决方案
在这里找到答案:Android foreground service notification not showing
这是奥利奥的问题,感谢@Yashaswi N P
【问题讨论】:
-
我尝试使用 ".setContentText("content")" 但不起作用
-
是的,我得到了 null,真正的内容在 remoteMessage.getNotification().getBody()
标签: android firebase push-notification firebase-cloud-messaging android-8.0-oreo