Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 应用程序关闭时的Firebase通知_Android_Firebase_Firebase Cloud Messaging_Firebase Notifications - Fatal编程技术网

Android 应用程序关闭时的Firebase通知

Android 应用程序关闭时的Firebase通知,android,firebase,firebase-cloud-messaging,firebase-notifications,Android,Firebase,Firebase Cloud Messaging,Firebase Notifications,我已经在Android应用程序中实现了Firebase通知。当我的应用程序正在运行时,通知将以我的自定义布局显示,但当应用程序未运行时,通知将以默认布局显示。当应用程序未运行时,如何将通知布局更改为我的布局。此外,我还存储共享首选项以允许用户切换通知。但当应用程序未运行时,无论如何都会显示通知。如何才能做到这一点 @Override public void onMessageReceived(RemoteMessage remoteMessage) { if(SettingsFragme

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

}

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

看到这个了吗

具有通知和数据负载的消息,包括后台和前台。在这种情况下,通知被传送到设备的系统托盘,数据有效负载在启动器活动的目的的附加部分中传送

如果在应用程序处于后台时,将
{data:“something”}(数据消息)
{notification:“something”}(显示消息)
一起使用
{data:“something”}(数据消息)
,则数据有效负载将传递给意图之外的对象,但不会传递给
onMessageReceived()
方法。我假设您实现了显示通知的代码,因此,当你的应用程序在前台时,
onMessageReceived()
是触发器,它会显示你想要的通知,但当它不是
onMessageReceived()
没有get触发器时,android系统会用你的通知负载处理它。您只需从服务器端代码中删除
{notification:“something”}(显示消息/通知托盘)
,即可始终确保
onMessageReceived()


对于任何人来说,在MessageReceived()上不断提及
总是会触发,无论它是前台还是后台,请访问此

有两种类型的FCM消息

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

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

    参考

    当应用程序关闭或在后台运行时,不应触发代码。见此:


    你需要做的是检查应用程序如何启动,点击通知或点击启动图标。为此,应将一些数据添加到通知中,然后在应用程序的第一个启动活动中检索这些数据。如果您能够成功检索它们,这意味着您的应用程序通过点击通知启动,然后您可以执行您想执行的操作。

    您的代码在哪里?我想了解技术信息,不想执行您是否使用firebase控制台发送消息?是的,我使用控制台。这就是原因。您必须使用服务器端代码来处理请求。据我所知,控制台总是使用通知有效负载。@Teckwi正确,您可以在控制台中发送的数据正好是通知的正文字段。文档中说,如果消息中发送了通知负载,那么FCM将代表客户端应用程序在设备上处理通知。