应用关闭时的Firebase通知 [英] Firebase notifications when app is closed

查看:155
本文介绍了应用关闭时的Firebase通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android应用程序中实施了Firebase通知。当我的应用程序正在运行时,通知将显示在我的自定义布局中,但是当应用程序未运行时,通知将以默认布局显示。当应用程序不运行时,如何将通知布局更改为我的布局。此外,我存储共享偏好设置,让用户切换通知。但是,当应用程序不运行时,通知显示无论如何。如何实现这一点?

I have implemented Firebase notification in my Android application. When my app is running, notification is displayed with my custom layout, but when application is not running, the notification is displayed with the default layout. How can I change the notification layout to my layout when application is not running. Also, I store shared preferences to let user toggle notifications. But when app is not running the notification is displayed anyways. How can achieve that?

@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());
    }

}


推荐答案

您的问题是您在通知栏中使用它。

Your problem is you using it with notification tray.

请参阅 link
$ b

See this link


消息包含通知和数据有效载荷,包括背景和前景。在这种情况下,通知被传送到设备的系统托盘,并且数据有效载荷被传送到您的启动程序Activity的目标的附加内容中。

Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.


$ b $如果你使用 {data:something}(data-message)并且使用 {notification:something}(display-message )当你的应用程序在后台时,数据有效载荷将被传递给意图的附加内容,但不会传递给 onMessageReceived()方法。你的代码显示通知,所以当你的应用程序在前台 onMessageReceived()是触发器,它显示你想要的通知,但是当它不是 onMessageReceived()没有得到触发器,而是android系统将处理它与您的通知有效载荷。您只需从服务器端代码中删除 {notification:something}(display-message / notification tray),以确保 onMessageReceived()

If you using {data:"something"}(data-message) with {notification:"something"}(display-message) while your app is in background the data payload will delivered to extras of the intent but not to the onMessageReceived() method.I assume you implement your code for showing notification, so when your app is in foreground onMessageReceived() is trigger and it display the desire notification you want but when it is not onMessageReceived() no get trigger instead android system will handle it with your notification payload. You just have remove {notification:"something"}(display-message/notification tray) from your server side code to always ensure onMessageReceived().

对于任何人不断提及 onMessageReceived()总是会触发不管它是请勿访问前台或后台,请访问链接

For anyone keep mention onMessageReceived() will always trigger no matter wether it is not foreground or background please visit this link

这篇关于应用关闭时的Firebase通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆