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

查看:29
本文介绍了应用关闭时的 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.

查看此链接

带有通知和数据负载的消息,包括后台和前台.在这种情况下,通知会传递到设备的系统托盘,而数据负载会在启动器 Activity 的 Intent 的附加部分中传递.

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.

如果您在应用运行时将 {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天全站免登陆