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

确定是否从FCM通知单击打开了活动

柴星津
2023-03-14

我正在使用fcm控制台向所有安装了我的应用程序的设备发送消息,通知没有任何额外的负载,只有通知消息。

我想知道是否有一种简单的方法可以知道活动是否从FCM通知点击打开。

有一种解决方案是扩展FirebaseMessagingService并自己创建通知。

我想知道是否有其他解决方案,而不扩展服务或向通知传递额外内容。

是否传递了任何额外信息,用于从通知单击打开活动?

共有3个答案

齐磊
2023-03-14

无需故意传递任何额外信息。只需将标志设置为

  Intent lIntent = new Intent(getApplicationContext(),YourActivity.class);
        lIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        lIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        lIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(lIntent);
夏飞掣
2023-03-14

如果您想在不扩展FireBaseMessaging类的情况下启动任何活动,则可以使用FCM数据负载设置此标志。

因此,每当用户单击系统托盘上的FCM通知时。该定义的活动应该会打开。如果您想获得额外的捆绑包data.you可以使用getIntent(). getExtras();方法获得它。

    "message":{
     "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
     "notification":{
       "title":"Match update",
       "body":"Arsenal goal in added time, score is now 3-0"
     },
"android":{
           "ttl":"86400s",
           "notification"{
             "click_action":"MainActivity.class" // define your activity here
           }
         },
 "payload": {
         "aps": {
           "category": "NEW_MESSAGE_CATEGORY"
         }
       }
     }

请参阅以下链接:-

"设置适当的键来定义用户点击Android和iOS通知的结果-click_action和类别。"

鲁彬炳
2023-03-14

我希望您知道,基于FCM的消息传递创建了两种类型的通知

首先,我们从ononMessageReceived()方法创建的通知,这里需要注意的一点是,如果应用程序位于前台,将触发onMessageReceived()

其次,当应用程序在后台时,FCM会创建自己的默认通知,在这种情况下,不会拦截onMessageReceived(),因此我们无法自行设置挂起的意图。

注意:当您向应用程序发送“通知”推送消息时,无论应用程序处于前台还是后台,都会触发me“数据”消息

回到你的问题,不清楚你是从FCM控制台发送推送消息还是发出FCM服务器请求,所以让我们在这里举例子。

  1. FCM控制台发送的消息:从FCM上通知面板的advance部分发送数据负载,如下所示

当应用程序位于前台时,将截获onMessageReceived(),使用下面的代码接收数据负载

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    //Displaying data in log
    //It is optional


    //Calling method to generate notification
    sendNotification(remoteMessage.getData());
}

//This method is only generating push notification
//It is same as we did in earlier posts
private void sendNotification(Map<String, String> messageBody) {

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

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(
            this,
            0,
            setNotificationRoute(messageBody),
            PendingIntent.FLAG_UPDATE_CURRENT);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(android.R.drawable.sym_def_app_icon)
            .setContentTitle(messageBody.get("title"))
            .setContentText(messageBody.get("text"))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

    notificationManager.notify(0, notificationBuilder.build());


}

private Intent setNotificationRoute(Map<String, String> messageBody) {
    Intent intent = null;
    String type = messageBody.get("type");
    if (type != null) {
        switch (type) {
            case "android":    //intercept your payload here to create swit 
                intent = new Intent(this, MainActivity.class);
                break;
            default:
                break;

        }

    }
    return intent;
}
}

如果应用程序位于后台,则在收到通知后,单击应用程序将在“默认”活动上打开,您可以通过在活动的意图过滤器的应用程序清单中添加以下行将任何活动设置为默认活动:

<category android:name="android.intent.category.DEFAULT" />

在这个活动中,你可以调用获取额外的意图,然后获取数据负载来决定你需要登陆的活动

    .
    .
    .

        Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
                setNotificationRoute(getIntent().getExtras());
    .
    .
}
 private void setNotificationRoute(Bundle extras) {
    String type = extras.getString("type");
    Intent intent = null;
    if (type != null) {
        switch (type) {
            case "message":
                String room = extras.getString("room");
                intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
      }
    }

从FCM服务器发送的消息:从上述FCM控制台发送的消息与将以下json正文作为post请求发送到FCM服务器相同:

{ 
    "notification": {
    "title": "Hi Tester",
    "text": "News for testing",
    "sound": "default",
    "badge": 0
  },
  "data":{
    "type": "credits"
  },
  "priority": "high",
  "to": "{{device_token}}"
}

在这种情况下,拦截通知的过程将与此相同。

 类似资料:
  • 我正在工作的应用程序中,我被要求显示通知。对于通知,我使用的是FireBase云消息(FCM)。我能够得到通知,当应用程序在后台。 2.)MyFirebaseInstanceIDService 这是我在MyFirebaseMessagingService类中为onMessageReceived()方法编写的代码示例。

  • 我有一个带有通知的应用程序,如果我单击它们,就会打开某个活动。我想要的是,如果我点击通知,活动已经打开,它不是重新开始,但只是出现在前面。 我可以用标志来管理它吗?还是我应该在SharedPreferences中存储一个变量来检查它是否已打开? 谢谢!

  • 我已经尝试了几乎每一个解决方案张贴在这里和每一个标志的组合,但它是不工作的。 当我在场景中单击来自后台模式的通知时,意图丢失在某个地方(未在创建时传递给)。 我也尝试传递唯一的id到我的待定意图,但没有运气。我花了很多时间和精力来解决这个问题,但每次都失败了 这是我的FCM代码 我的清单文件

  • 对于如何使用Firebase通知和数据,应该有明确的实现。我读了很多答案,但似乎不能使它起作用。以下是我的步骤: 1.)我正在用PHP向android传递通知和数据,看起来似乎没问题: 如果有人能指导或建议我如何在MainActivity.java中从Firebase中检索数据,无论场景(前台还是后台),这都是很棒的。

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