Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.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通知在后台,意图数据为空_Android_Notifications - Fatal编程技术网

应用程序运行期间的Android通知在后台,意图数据为空

应用程序运行期间的Android通知在后台,意图数据为空,android,notifications,Android,Notifications,我正在使用这个代码,当应用程序在前台时,它工作得非常好。但在应用程序运行期间发出的通知是后台的,没有任何数据 Intent intent = new Intent(this, SplashActivity.class); intent.putExtra(Constant.device_id,deviceId); intent.putExtra(Constant.isFromPush,true); intent.addFlags( Intent.FLAG_ACTIVIT

我正在使用这个代码,当应用程序在前台时,它工作得非常好。但在应用程序运行期间发出的通知是后台的,没有任何数据

 Intent intent = new Intent(this, SplashActivity.class);

    intent.putExtra(Constant.device_id,deviceId);
    intent.putExtra(Constant.isFromPush,true);
    intent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_id);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(title_)
                    .setContentText(message)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                getString(R.string.app_name),
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    notificationManager.notify(new Random().nextInt( 2000 ) /* ID of notification */, notificationBuilder.build());

若要在应用程序处于后台时处理意图数据,您需要做一些额外的事情。 在您的响应中应该有“数据”键,以便在活动中获得它。像

{
"notification": {
    "key_1": "value_1",
    "key_2": "value_2"
 },
"data": {
    "key_1": "value_1",
    "key_2": "value_2"
 },

}
您需要获取启动活动的onCreate方法中的值。 启动活动是
包含的活动

 <category android:name="android.intent.category.LAUNCHER" />
在后台,应用程序在通知托盘中接收通知负载,并且仅在用户点击通知时处理数据负载


我们有两种类型的有效负载,在发送下行的情况下都是可选的

数据

此参数指定消息有效负载的自定义键值对

通知

此参数指定通知有效负载的预定义的、用户可见的键值对

{
  "notification": {
        "title" : "title",
        "body"  : "body text",
        "icon"  : "ic_notification",
        "click_action" : "OPEN_ACTIVITY_1"
       }
}
[此处有更多详细信息]

当您在后台时,FCM将根据通知负载的信息在系统托盘中显示通知。系统托盘上用于通知的标题、消息和图标来自通知负载

{
  "notification": {
        "title" : "title",
        "body"  : "body text",
        "icon"  : "ic_notification",
        "click_action" : "OPEN_ACTIVITY_1"
       }
}
您需要使用数据有效负载而不是通知有效负载,您的问题将得到解决

下面是我收到的JSON示例:

{
  "to": "FCM registration ID",

   "data": {
     "someData"  : "This is some data",
     "someData2" : "etc"
   }
}
这是我的java代码

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        if (remoteMessage == null)
            return;

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {

            try {
                JSONObject json = new 
                JSONObject(remoteMessage.getData().toString());
                handleDataMessage(json);
            } catch (Exception e) {
                Log.e(TAG, "Exception: " + e.getMessage());
            }
        }
    }
同时包含通知和数据负载的消息:

消息还可以包含通知和数据有效负载。发送此类消息时,将根据应用程序状态(后台/前台)在两种情况下进行处理。对于这些消息,我们可以同时使用通知键和数据键

在后台时–应用程序在通知托盘中接收通知负载,并且仅在用户点击通知时处理数据负载


在前台时–应用程序接收到一个消息对象,其中两个有效负载都可用。

显示您的SpalshActivity代码。@覆盖受保护的void onResume(){super.onResume();isFromPush=getIntent().getBooleanExtra(Constant.isFromPush,false);字符串dev=”“;dev=getIntent().getStringExtra(Constant.device_id);if(isFromPush){Log.e(“from push”,“Yes”+dev)}else{Log.e(“from push”,“No”+dev)}让我知道它是否工作->覆盖fun onNewIntent(intent:intent?{super.onNewIntent(intent)//将代码放在这里}不工作,感谢您的响应是否包含数据键?推送消息中必须同时包含“通知”和“数据”键。请尝试使用
意图过滤器
启动器处理活动中的意图。默认情况下,它从通知中获取
数据。如果您使用启动屏幕,只需将
意图
复制到您需要的活动中即可data@YaroslavOvdiienko你能用几行代码解释一下吗。感谢检查您的清单,该活动包含android.intent.category.LAUNCHER,然后在该活动的onCreate方法中,使用Bundle Bundle=getIntent().getExtras()获取额外信息;如果(bundle!=null){//bundle包含通知}的“data”字段的所有信息以获取key_1值,那么它应该是getIntent().getExtras().getString(“key_1”)。非常感谢,我只是从remoteMessage.getNotification()解析该值。