当前位置: 首页 > 知识库问答 >
问题:

在前台接收到的firebase通知上的打开活动

微生慈
2023-03-14

但是当它在后台时打开应用程序,我需要在我的应用程序在前台时打开它。

从firebase文档中可以看到:

当应用程序处于后台时传递的通知。在这种情况下,通知被传递到设备的系统托盘。默认情况下,用户点击通知将打开应用程序启动器。同时具有通知和数据负载的消息,包括后台和前台。在这种情况下,通知被传送到设备的系统托盘,而数据有效载荷被传送到启动程序活动意图的附加部分中。

这是我对OnMessageReceived的含义

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

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

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
            sendNotification( remoteMessage);              
        }     
    }

    /**
     * Create and show a simple notification containing the received FCM message.
     *
     * @param remoteMessage FCM message message received.
     */
    private void sendNotification(RemoteMessage remoteMessage) {
        Intent intent = new Intent(this, MyActivity.class);

        Map<String, String> hmap ;
        hmap = remoteMessage.getData();
        hmap.get("data_info");
        intent.putExtra("data_info", hmap.get("data_info"));
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);


        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

    }

我能够正确获取通知,但只有在点击系统托盘上的通知后,活动才会开始。

有没有一种方法可以在前台时不点击通知就开始活动?

扩展FireBaseMessagingService的类MyFireBaseMessagingService中的方法OnMessageReceived()在前台时被正确调用,但活动尚未开始。我还尝试使用标志flag_activity_new_task也没有成功。提前道谢。

共有1个答案

晏经武
2023-03-14

您可以在前台活动中注册广播接收器,并从onReceiveMessage()方法发送广播。

前景活动

mReceiver = new BroadcastReceiver() {
 @Override
 public void onReceive(Context context, Intent intent) {
     Intent myNewActivity = new Intent(this, MyActivity.class);
     startActivity(myNewActivity);
   }
 };

mIntentFilter=new IntentFilter("OPEN_NEW_ACTIVITY");

@Override
protected void onResume() {
     super.onResume();
     registerReceiver(mReceiver, mIntentFilter);
}



@Override
protected void onPause() {
     if(mReceiver != null) 
            unregisterReceiver(mReceiver);
            mReceiver = null;
     }
     super.onPause();
   }

FirebaseNotificationReceiver

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

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

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        sendNotification( remoteMessage);  

        Intent broadcast = new Intent();
        broadcast.setAction("OPEN_NEW_ACTIVITY);
        sendBroadcast(broadcast);
    }     
}
 类似资料:
  • 我知道我可以设置click_action,但这只是为了定制哪些活动将在通知点击时启动,我需要一些将在通知收到时自动启动的东西。 我尝试了quick start messaging firebase示例,其中有一个onMessageReceived()方法,但它仅在应用程序处于前台时才起作用。是否有一些东西将执行同时应用程序在后台?GCM可以通过直接从接收到通知时调用的广播接收器启动活动意图来完成我

  • 我已经尝试了所有的方法,但它不适合我。我想打开或恢复应用程序,无论屏幕打开,同时单击通知。 我使用了以下方法:

  • 我看到了一个Firebase控制台的代码:如何为通知指定click_action,我使用了该通知,但在初始化变量CLS时出现了一个错误。我试图通过定义CLS=NULL来解析,以清除错误。它无法使用click_action打开我指定的活动 拜托,我有没有搞错什么?我怎么才能让它起作用?

  • 我有一个包含多个活动的应用程序。我正在植入Firecbase FCM,除了在接收到通知图标时处理用户单击通知图标时转到哪个活动之外,它都运行得很好。 由于我不确定FirebaseMessagingService是如何/何时实例化的,我无法将其设置为访问我的任何对象。我唯一能做的就是将应用程序中的当前活动存储为 然后,我在每个活动的OnCreate()中设置上述内容,然后在Firebase Mess

  • 我引用了前台和后台接收firebase通知的答案-->https://stackoverflow.com/A/38451582/12553303 其实下面是我的疑问:-------- 如果我没有为前台条件编码(谈论推送通知),当我的应用程序在后台时,我仍然会得到通知,对吗????-->是的 但是,当im处于前台状态时,我从firebase推送了一个通知-->我不会在状态栏上看到通知,这也是可以的(

  • firebase工作得很好,在状态栏上推送通知,但我的挑战是当通知被点击时,我希望它带我到我的自定义活动而不是默认的启动器,我该怎么做呢? 这是我想在通知计时时打开的自定义活动。通知来自Firebase cloud Messaging。 } 这是侦听来自FireBase的通知的服务。