ork*_*kun 22 android firebase firebase-cloud-messaging firebase-notifications
我在Android应用程序中实现了Firebase通知.当我的应用程序运行时,将显示包含我的自定义布局的通知,但是当应用程序未运行时,通知将以默认布局显示.如何在应用程序未运行时将通知布局更改为我的布局.此外,我存储共享首选项以允许用户切换通知.但是当应用程序未运行时,无论如何都会显示通知.怎么能实现呢?
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if(SettingsFragment.getReceiceNotification()){ //if user wants to receive notification
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.push_notification_layout);
remoteViews.setImageViewResource(R.id.push_notif_icon,R.mipmap.ic_bird_black);
Intent intent = new Intent(this,MainActivity.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);
notificationBuilder.setContent(remoteViews);
notificationBuilder.setContentTitle("Radyo Türkku?u");
notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setContentIntent(pendingIntent);
remoteViews.setTextViewText(R.id.push_title, "Radyo Türkku?u");
remoteViews.setTextViewText(R.id.push_context, remoteMessage.getNotification().getBody());
//notificationBuilder.setLights (ContextCompat.getColor(MainActivity.context, R.color.pushColor), 5000, 5000);
notificationManager.notify(0,notificationBuilder.build());
}
}
Run Code Online (Sandbox Code Playgroud)
tec*_*wei 27
您的问题是您使用通知托盘.
看到这个链接
具有通知和数据有效负载的消息,包括后台和前台.在这种情况下,通知将传递到设备的系统托盘,并且数据有效负载将在启动器活动的附加内容中传递.
如果您在应用程序处于后台时使用{data:"something"}(data-message),{notification:"something"}(display-message)则数据有效负载将传递给intent的额外内容,但不会传递给onMessageReceived()方法.我假设您实现了显示通知的代码,因此当您的应用程序位于前台时onMessageReceived()触发并显示所需你想要的通知,但是当它不是onMessageReceived()没有获得触发器时,android系统将使用你的通知负载来处理它.您只需{notification:"something"}(display-message/notification tray)从服务器端删除代码即可始终确保onMessageReceived().
对于任何人,一直提到onMessageReceived()将始终触发,无论它不是前景或背景,请访问此链接
Shs*_*ong 11
有两种类型的FCM消息
从Firebase控制台发送的邮件是通知邮件.为了在onMessageReceived()中获取消息,请使用数据消息.使用服务器端的以下代码发送数据通知消息
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
},
}
Run Code Online (Sandbox Code Playgroud)
参考 https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
| 归档时间: |
|
| 查看次数: |
31800 次 |
| 最近记录: |