GCM推送通知,通知点击事件

时间:2017-05-21 08:41:58

标签: android google-cloud-messaging

根据api发送通知的时候。我在清单中传递了click_action和config。但我不知道在哪里可以获得点击事件。我是Android开发的新手。我想要的是在点击通知时获得点击回调。那我接下来该怎么办?我在网站上搜索了一些答案,但没有人说出一些细节。希望有些人可以帮助我。感谢

2 个答案:

答案 0 :(得分:1)

您需要在AndroidManifest.xml中为想要在点击通知时打开的活动添加一个intent过滤器。

例如:

    <activity android:name=".SomeActivity">
        <intent-filter>
            <action android:name="SOME_ACTION"/>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

如果消息有效负载包含(在&#34;通知&#34;字典中)以下内容,则单击通知将打开该活动:

"click_action":"SOME_ACTION"

如果要在单击通知时运行某些逻辑,则应将该逻辑放在将要打开的活动的onCreate()方法中。

答案 1 :(得分:0)

在Android上处理通知点击事件本身没有回调。相反,使用的是创建通知时包含的PendingIntent

要实现我认为您尝试实现的目标,即采取通知正文中click_action参数中建议的不同操作,您可能需要在{{1}中处理此问题您的类的方法,它扩展了onMessageReceived(根据docs)。

您可以使用此代码块来处理远程消息:

FirebaseMessagingService

然后,您可以使用private void sendMessage(RemoteMessage remoteMessage) { if (remoteMessage.getData() > 0) { String action = remoteMessage.getData().get("click_action"); // if you sent the notification with a "notification body", you can do this String notificationBody = remoteMessage.getNotification().getBody(); // create notification with the action and the notification body createNotification(action, notificationBody); } } private void createNotification(String action, String notificationBody){ // create the intent and set the action Intent intent = new Intent(getApplicationContext(), SomeActivity.class); intent.setAction(action); // create a pending intent PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // build the notification NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText(notificationBody) .setContentIntent(pendingIntent); //set the content intent NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // notificationId allows you to update the notification later on. mNotificationManager.notify(notificationId, mBuilder.build()); } 继续检索SomeActivity中的具体操作,并处理每个操作案例。

总之,你真的找不到一个&#34;点击听众&#34;在Android中发送通知。

您可以查看以下链接:https://developer.android.com/guide/topics/ui/notifiers/notifications.htmlhttps://firebase.google.com/docs/cloud-messaging/android/receive,以进一步了解该过程。

干杯。