应用程序停止时从FCM启动活动?

时间:2016-08-30 12:24:29

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

我正在使用 Firebase云消息传递并希望显示呼叫。 firebase消息已成功接收且通知部分有效,但当应用程序关闭且 DEAD <时,我无法从 FirebaseMessagingService 中启动 ShowCallActivity /强>

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    if(remoteMessage.getNotification().getBody().equals("showcall")) {
            Log.e(TAG,"Want to start activity");
            Intent intent = new Intent(this,ShowCallActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
        }
    }

}
// 

显示通知但不会打开死应用程序。

3 个答案:

答案 0 :(得分:0)

Intent resultIntent = new Intent(this, MainActivity.class);
        resultIntent.putExtra("msg", name);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
                resultIntent, PendingIntent.FLAG_ONE_SHOT);

在发送通知中使用此功能,并将MainActivity替换为您要打开的类

答案 1 :(得分:0)

要使其正常运行,我必须使用数据消息并保持"notification"键为空。

firebase文档中的示例。

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

答案 2 :(得分:0)

我是编程新手,终于找到了有关此问题的一些线索。 android(MarshMallow)的新版本具有新的限制,该限制限制了当应用程序停止运行时必须从应用程序打开的活动。我无法解释太多,但您可以在这里阅读 https://developer.android.com/guide/components/activities/background-starts?authuser=1 如果授予此权限,则每个意图都可以正常工作。您可以检查任何VOIP应用程序,例如WhatsApp。该应用程序已经在用户不知情的情况下授予了此权限。希望有帮助