2016-12-02 40 views
2

我已将FCM集成到了我的应用程序中,我可以在应用程序运行或死亡等情况下收到通知。但是如果应用程序正在运行,那么我可以导航特定的屏幕。但是如果应用程序死亡或关闭,那么如果我点击了通知,那么总是将其重定向到主屏幕而不是导航区域。 这是我使用的代码:FCM导航无法在应用程序死亡或背景时工作。

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 

    // Check if message contains a data payload. 
    // if (remoteMessage.getData().size() > 0) { 
    L.d(TAG, "Message data payload: " + remoteMessage.getData()); 


    // if (remoteMessage.getNotification() != null) { 
    String msg = remoteMessage.getNotification().getBody(); 

    if (!TextUtils.isEmpty(msg)) { 

     sendNotification(remoteMessage.getData(), msg); 
    } 
} 
private void sendNotification(Map<String, String> data, String messageBody) { 

    String referenceKey = data.get("ReferenceKey"); 
    String referenceValue = data.get("ReferenceValue"); 

switch (referenceKey) { 
       case Repository.ModuleCode.BRAND: 
         intent = new Intent(this, WebViewActivity.class); 
         intent.putExtra("ID", referenceValue); 
         intent.putExtra("browser", false); 
        break; 

       case Repository.ModuleCode.NEWS: 
         intent = new Intent(this, NewDetailActivity.class); 

        break; 

        } 
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    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_notification) 
      .setContentTitle(getString(R.string.app_name)) 
      .setContentText(messageBody) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

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

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

在清单:在应用摇篮

<service 
     android:name=".fcm.MyFirebaseMessagingService" 
     android:enabled="true" 
     android:exported="true"> 

     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
     </intent-filter> 
    </service> 

    <service android:name=".fcm.MyFirebaseInstanceIDService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
     </intent-filter> 
    </service> 

compile 'com.google.android.gms:play-services:9.6.1' 
compile 'com.google.android.gms:play-services-maps:9.6.1' 

,如果应用程序被杀害或仅关闭情况下,我无法导航的确切网页。 在此先感谢

回答

3

最后我从后端更改了Payload,然后它解决了我的问题。

以前我使用的有效载荷这样

{ "notification": { 
"text": "Your Text1" 
}, 
"data": { 
"ReferenceKey": "PR" , 
"ReferenceValue": "10," 
}, 
"to" : "pushtoken" 
} 

然后我删除通知并这样使用。

{ 
"data": { 
"Message": "Test", 
"ReferenceKey": "PR" , 
"ReferenceValue": "10," 
}, 
"to" : "pushtoken" 
} 

然后它的工作前景/杀死/关闭。

之后,我只能在xiaomi note3中收到通知。

+1

查看小米笔记中的网络3.可能是因为某些网络错误.. –

+0

小米HM注意1 LTE android 4.4.4不工作.. – AngelJanniee

+0

通过手动或从程序启用自动启动权限勾选此链接https:// stackoverflow.com/questions/39368251/how-to-enable-autostart-option-for-my-app-in-xiaomi-phone-security-app-programma。 – Sandeep

2

它按预期工作..onMessageReceived不会触发,如果应用程序被杀害或在后台。如果您的应用程序在后台或关闭然后通知消息显示在通知中心,任何来自该消息的数据被传递给由于用户点击通知而启动的意图。

您可以使用getIntent().getExtras();在启动时获取意图以获取意图。

更多信息here;

如:

 Bundle bundle = getIntent().getExtras(); 
     if (bundle != null) { 
      if (bundle.containsKey("data")) { 
      Intent intent = new Intent(mContext, ExpectedActivity.Class) 
      intent.putExtras("PUSH_KEY",bundle.get("data").toString()); 
      startActivity(intent) 
     } 
     } 

将此代码放在你的发射activity.And这将引导您到您的预期活动即使该应用程序被杀害或者是在后台。

或者

您可以致电通知点击自定义活动,如果您的应用程序在后台通过调用火力消息REST服务API这里https://stackoverflow.com/a/37599088/3111083给出。

+0

我已经完成了上面的代码,它也在工作,我也可以......我可以按预期得到通知。但我的情况下,如果我的应用程序关闭或死亡。那么我可以得到通知。但是如果我点击那个没有重定向到预期页面的通知。但如果应用程序正在运行,那么它将导航到预期的页面。 – AngelJanniee

+0

@AngelJanniee不重定向到期望页面的原因是因为通知数据未收到“onMessageReceived”。您正在重定向到“onMessageReceived”的预期页面。您也可以从您的启动页面执行相同的操作。 –

+0

好的。我会做和测试,但如果应用程序正在运行,那么我可以在通知栏中获取通知。但是如果我点击任何通知,那么它只会重定向到最新收到的项目。然后如果我点击其他通知图标,那么它不会调用任何东西 你可以建议这个问题?? – AngelJanniee

0

onMessageReceived仅当您的应用程序位于前台时才起作用,如果应用程序位于后台或将其终止,则该通知将随着标题和消息正文而膨胀。如果您点击通知,它会引导您进入应用程序启动器活动,并且可以从onCreate中的getIntent中检索通知的数据对象。

数据的对象键/值已经成为意图中的serilies,因此只需从数据对象中用键/ varible提取数据即可。

例如:

Bundle extras = getIntent().getExtras(); 
String msg = extras.getString("time"); 

注意,如果点击了通知你只能得到这些数据。

相关问题