Android GCM推送通知、通知单击事件
当我根据api发送通知时。我在清单中传递了click_操作和配置。但我不知道在哪里可以得到点击事件。我是安卓开发的新手。我想要的是在单击通知时获得单击回调。那我下一步该怎么办?我在网站上搜索了一些答案,但没有一个能告诉我细节。希望有人能帮我。感激您需要在AndroidManifest.xml中为您希望在单击通知时打开的活动添加一个意图过滤器 例如:Android GCM推送通知、通知单击事件,android,google-cloud-messaging,Android,Google Cloud Messaging,当我根据api发送通知时。我在清单中传递了click_操作和配置。但我不知道在哪里可以得到点击事件。我是安卓开发的新手。我想要的是在单击通知时获得单击回调。那我下一步该怎么办?我在网站上搜索了一些答案,但没有一个能告诉我细节。希望有人能帮我。感激您需要在AndroidManifest.xml中为您希望在单击通知时打开的活动添加一个意图过滤器 例如: <activity android:name=".SomeActivity"> <intent-filte
<activity android:name=".SomeActivity">
<intent-filter>
<action android:name="SOME_ACTION"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
如果希望在单击通知时运行某些逻辑,则应将该逻辑放入将打开的活动的onCreate()
方法中。Android上没有用于处理通知单击事件的回调本身。相反,在创建通知时,使用的是一个包含
要实现我认为您正试图实现的目标,即按照通知正文中的click_action
参数中的建议采取不同的操作,您可能需要在类的onMessageReceived
方法中处理此问题,该方法扩展了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());
}
然后,您可以使用getIntent().getAction()
继续检索SomeActivity
中的特定操作,并处理每个操作案例
总之,在Android中,你真的找不到通知的“点击侦听器”
您可以查看以下链接:并进一步了解流程
干杯。谢谢您的回复,事实上我做到了您所说的,在my manifest.xml中添加了一个意图过滤器,并通过了单击操作。但是我不知道我应该用onCreate方法写什么样的代码。我不知道android代码,我被要求完成这个函数,所以我应该先完成它。那么,您能否提供一个示例代码,比如我应该在onCreate方法中编写的代码?多谢各位much@wuzinong这取决于在单击通知后打开活动时应用程序应该执行的操作。例如,它可能读取消息的“数据”字典中传递的一些自定义数据并对其进行处理。是的,我将消息放入数据中,如{data:{message:“a”,title:“b”},notification:{click_action:“some_action”},然后调用api传递数据。在android方面,我可以获得这些数据,但我不知道如何在我的android中捕捉点击功能code@wuzinong你说的“点击功能”是什么意思?你的意思是获得“一些行动”的价值吗?你为什么需要它?Android已经使用它来打开所需的活动。好吧,我只是有点困惑。我注册了一些动作,一定有地方我应该写一些动作函数。我不知道该写在哪里,应该写什么。也许我应该从一开始就学习android开发,但只是因为这个函数很紧急,目前没有时间学习。我使用micrisoft azure notification hub的api发送消息网站:我看到NotificationsHandler中有一个onReceive方法,当收到通知时会调用它。我想知道它是否具有类似于onNotificationClick的功能,因此我可以处理通知单击功能,并扩展了com.microsoft.windowsazure.notifications.NotificationsHandler此类,但在此父类中找不到sendMessage或createNotification这些功能。对于PendingEvent,也无法获得参考。因为我们希望使用microsoft azure通知中心来管理所有平台的推送通知。有没有其他方法我可以用?
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());
}