【发布时间】:2016-09-16 10:36:08
【问题描述】:
昨天,Google 在 Google I/O 上展示了基于新 Firebase 的新通知系统。我用 Github 上的示例尝试了这个新的 FCM(Firebase Cloud Messaging)。
通知的图标始终是 ic_launcher,尽管我已经声明了一个特定的可绘制对象
为什么? 下面是处理消息的官方代码
public class AppFirebaseMessagingService extends FirebaseMessagingService {
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
sendNotification(remoteMessage);
}
// [END receive_message]
/**
* Create and show a simple notification containing the received FCM message.
*
* @param remoteMessage FCM RemoteMessage received.
*/
private void sendNotification(RemoteMessage remoteMessage) {
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);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// this is a my insertion looking for a solution
int icon = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.drawable.myicon: R.mipmap.myicon;
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(icon)
.setContentTitle(remoteMessage.getFrom())
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
【问题讨论】:
-
firebase 与您创建通知的方式无关,请提供您所看到内容的图片
-
准确。此代码直接来自 Firebase,并且 sendNotification() 方法对于任何通知都完全相同。此代码适用于 GCM,但适用于 FCM 编号。它始终保持 ic_launcher,使用新的 Web 界面发送消息
-
你设置的是小图标而不是大图标,除非你在推送有效载荷中发送带有通知标签的推送,它与 FCM 无关
-
当应用程序在前台时是否显示您的自定义通知图标?这对我行得通。但是,当应用程序在后台时,它必须使用某种默认的 FCM 处理程序,因为所有通知设置都会被忽略(图标、声音、灯光、振动等无法自定义)。
标签: android push-notification firebase googleio firebase-cloud-messaging