2016-11-21 101 views
7

我正在使用Firebase云消息传递将数据消息传递到正在开发的应用程序。根据FCM文档,当应用程序处于前景时:Firebase云消息传递 - Android:关闭应用时的通知的点击操作不起作用

客户端应用程序在onMessageReceived()中接收数据消息,并可以相应地处理键值对。

它工作正常。当应用程序在后台,该行为是不同的:

数据有效载荷可以用于启动你的活动意图进行检索。

而且它似乎不能很好地工作。 我在通知JSON中使用了一个“click_action”参数,其中我指定了我要打开的“activity”标记内的Android Manifest中使用的intent过滤器的名称。例如,我的通知可能是:

{ 
    "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...", 
    "notification" : { 
     "body" : "This is a test", 
     "title" : "Test", 
     "click_action" : "com.test.click" 
    }, 
    "data" : { 
     "key" : "data" 
    } 
} 

而且我的表现有这个意图过滤器,在“MainActivity”:

<intent-filter> 
    <action android:name="com.test.click" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 

当我的应用程序在后台,我启动的通知,一切都似乎运作良好:我单击通知并显示正确的活动,并且数据密钥位于收到的意向的“附加”中。 当我的应用程序关闭时出现问题:当我点击通知时,应用程序正确启动并使用“com.test.click”意图过滤器打开活动,但是如果我尝试发送另一个通知,没有任何反应,并且在我重新启动应用程序之前,后台通知不再有效。

更清楚:当我的应用程序处于前景或背景时,通知可以正常工作。如果我的应用已关闭,则会正确处理第一个通知,但是在重新启动应用之前,其他所有后台通知都不起作用:如果我点按通知,我的应用会显示最后一次观看的活动,就好像我从中选择了应用“最近的应用程序”菜单。如果我关闭并重新启动应用程序,通知将恢复正常工作。

这是Android中的数据消息还是FCM问题?有没有办法解决这个问题?

我正在使用Nexus 5 API 23进行测试。

+1

你好,你有解决这个问题吗? – Bahu

+0

@Bahu不幸的是,我找到了一个只使用数据信息的解决方案,使用系统托盘通知这个错误依然存在。我认为这是一个FCM错误。你是否尝试过我在我的问题中描述的同样的错误? –

+1

是的兄弟,我试图解决这个问题。如果您对“数据消息,使用系统托盘通知”以外的任何建议请告诉我 – Bahu

回答

8

我会尽力来形容它是如何工作的我来说,

通知有效载荷例如:

{ 
"notification": { 
    "body": "This is a test message", 
    "title": "Message", 
    "click_action" : "OPEN_ACTIVITY_1" 
}, 
"data": { 
    "key": "test key" 

} 

清单在活动

<intent-filter> 
    <action android:name="OPEN_ACTIVITY_1" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 

当应用程序在后台或关闭FCM将创建一个“通知消息”,onClick会打开正确的活动,然后在应用程序关闭时处理通知,在onCreat中调用getIntent() e()方法

例如。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_hub); 

    handleIntents(getIntent()); //for simplicity I'm passing the value forward 


} 

当应用程序是我创建的方法的自定义通知前台onMessageReceived我FirebaseMessagingService。

例如。

Intent intent = new Intent(this, HubActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intent.putExtra("url", remoteMessage.getData().get("url")); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle("Message") 
      .setContentText("This is a test message") 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

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

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 

并在活动中适当地处理它我重写方法onNewIntent(Intent intent)。

例如。

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 

    handleIntents(intent); //for simplicity I'm passing the value foward 


} 

所以我认为你缺少的是onNewIntent(意向意图),因为已创建活动时,该处理的新意图。

相关问题