【问题标题】:Firebase notifications when app is closed应用关闭时的 Firebase 通知
【发布时间】:2017-05-13 23:42:02
【问题描述】:

我已经在我的 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());
    }

}

【问题讨论】:

  • 你的代码在哪里?
  • 我要的是技术信息,不想实现

标签: android firebase firebase-cloud-messaging firebase-notifications


【解决方案1】:

您的问题是您将它与通知托盘一起使用。

看到这个link

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

如果您在应用程序处于后台时将{data:"something"}(data-message) 与{notification:"something"}(display-message) 一起使用,则数据有效负载将传递给意图的额外部分,而不是onMessageReceived() 方法。我假设您实现了显示通知的代码,所以当你的应用程序在前台onMessageReceived() 是触发器,它会显示你想要的通知,但是当它不是onMessageReceived() 时没有获取触发器,Android 系统将使用你的通知有效负载处理它。您只需从服务器端代码中删除 {notification:"something"}(display-message/notification tray) 以始终确保 onMessageReceived()。

如果有人一直提到onMessageReceived(),无论它不是前景还是背景,都将始终触发,请访问此link

【讨论】:

  • 您是否使用 Firebase 控制台发送消息?
  • 是的,我确实使用控制台。
  • 这就是为什么。您必须使用服务器端代码来处理请求。据我所知,控制台总是使用通知负载。
  • @teckwei 没错,你可以在控制台发送的数据是通知的正文字段
【解决方案2】:

有两种类型的 FCM 消息

  1. 通知消息
  2. 数据消息

从 Firebase 控制台发送的消息是通知消息。为了在 onMessageReceived() 中获取消息,请使用 Data Message。在服务器端使用以下代码发送数据通知消息

{
   "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
   "data" : {
     "Nick" : "Mario",
     "body" : "great match!",
     "Room" : "PortugalVSDenmark"
   },
 }

参考https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

【讨论】:

  • 没错。文档说,如果在消息中发送了通知有效负载,那么 FCM 将代表客户端应用处理设备上的通知。
【解决方案3】:

当您的应用关闭或在后台运行时,您的代码不应被触发。看到这个:Firebase Notification

您需要做的是检查应用程序的启动方式,点击通知或点击启动器图标。为此,应将一些数据添加到通知中,然后在应用程序的第一个启动活动中检索它们。如果你能成功找回它们,说明你的应用通过点击通知启动,然后你就可以做你想做的事了。

【讨论】:

    猜你喜欢
    • 2018-08-31
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2018-07-05
    • 2014-08-10
    相关资源
    最近更新 更多