mar*_*rco 118 android push-notification googleio firebase firebase-cloud-messaging
昨天谷歌在Google I/O上展示了基于新Firebase的新通知系统.我在Github上尝试了这个新的FCM(Firebase云消息传递)示例.
尽管我已声明了特定的drawable,但通知的图标始终是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());
}
}
Run Code Online (Sandbox Code Playgroud)
Ian*_*ber 240
不幸的是,这是SDK 9.0.0-9.6.1中Firebase通知的限制.当应用程序在后台时,启动器图标将从清单中使用(具有必要的Android着色),用于从控制台发送的消息.
但是,使用SDK 9.8.0,您可以覆盖默认值!在AndroidManifest.xml中,您可以设置以下字段来自定义图标和颜色:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/notification_icon" />
<meta-data android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/google_blue" />
Run Code Online (Sandbox Code Playgroud)
请注意,如果应用程序位于前台(或发送数据消息),您可以完全使用自己的逻辑来自定义显示.如果从HTTP/XMPP API发送消息,您也可以始终自定义图标.
gee*_*aul 33
使用服务器实现向客户端发送消息并使用消息的数据类型而不是消息的通知类型.
这将帮助您获得回调,onMessageReceived
无论您的应用程序是在后台还是前台,然后您可以生成自定义通知
他们正在解决这个问题https://github.com/firebase/quickstart-android/issues/4
当您从Firebase控制台发送通知时,默认情况下会使用您的应用图标,并且Android系统会在通知栏中将该图标变为纯白色.
如果您对该结果不满意,则应该实施FirebaseMessagingService并在收到消息时手动创建通知.我们正在努力改善这一点,但目前这是唯一的方法.
编辑:用SDK 9.8.0添加到AndroidManifest.xml
<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/my_favorite_pic"/>
Run Code Online (Sandbox Code Playgroud)
我的解决方案类似于ATom的解决方案,但更容易实现.您不需要创建一个完全隐藏FirebaseMessagingService的类,您可以只覆盖接收Intent的方法(这是公共的,至少在版本9.6.1中),并从附加内容中显示信息."hacky"部分是方法名称确实是混淆的,并且每次将Firebase sdk更新为新版本时都会更改,但您可以通过使用Android Studio检查FirebaseMessagingService并查找需要的公共方法来快速查找一个Intent作为唯一的参数.在版本9.6.1中,它被称为zzm.这是我的服务的样子:
public class MyNotificationService extends FirebaseMessagingService {
public void onMessageReceived(RemoteMessage remoteMessage) {
// do nothing
}
@Override
public void zzm(Intent intent) {
Intent launchIntent = new Intent(this, SplashScreenActivity.class);
launchIntent.setAction(Intent.ACTION_MAIN);
launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* R equest code */, launchIntent,
PendingIntent.FLAG_ONE_SHOT);
Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(rawBitmap)
.setContentTitle(intent.getStringExtra("gcm.notification.title"))
.setContentText(intent.getStringExtra("gcm.notification.body"))
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
129670 次 |
最近记录: |