【发布时间】:2018-03-02 16:03:03
【问题描述】:
我正在从 Firebase 控制台向我在模拟器上运行的应用发送推送通知消息。
MyFirebaseMessagingService 类如下所示:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
if(remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
if(remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
Intent intent = new Intent(this, SplashActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "my_channel_01");
notificationBuilder.setContentTitle("FCM Notification");
notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher_round);
notificationBuilder.setContentIntent(pendingIntent);
notificationBuilder.setChannelId("my_channel_01");
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
API 26 的 NotificationCompat.Builder 的构造函数现在采用两个参数,一个是 Context,另一个是 String channelId。所以我只是为我的频道分配了一个随机字符串。
但是当我从 firebase 控制台发送消息时,模拟器上的应用程序给我一个错误 TOAST 说:
Failed to post notification on channel "my_channel_01"
我做错了什么?
【问题讨论】:
-
可能是因为您使用的图片不包含 Play 商店。
-
但是我在 logcat 中收到了消息。另外,我的图片是带有 Android 8.0(Google API)的 nexus 5s
标签: android firebase firebase-cloud-messaging