当我收到'通知'GCM消息时,android会自动从标题和正文创建通知,并将其放入通知阴影中。
当我在这里说“通知”时,我指的是本文档https://developers.google.com/cloud-messaging/concept-options#notifications_and_data_messages
中的通知我遇到的问题是,当我点击它时,没有任何反应 - 它不会启动我的活动,也不会取消通知。
我尝试过使用“click_action”选项,但这没有效果。 我添加了一个“数据”部分,但没有效果。
当我的应用程序处于前台时,我按预期获得onMessageReceived,并且我构建的NotificationCompat会按预期重新启动应用程序。
获取后台通知以启动应用程序我缺少什么?
这是清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="example.package"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<!-- [START gcm_permission] -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- [END gcm_permission] -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<permission
android:name="example.package.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="example.package.permission.C2D_MESSAGE" />
<application
android:name="example.package.application.appApplication"
android:allowBackup="true"
android:icon="@drawable/app_app_icon"
android:label="@string/app_name"
android:theme="@style/AppTheme"
tools:replace="android:icon, allowBackup"
>
<activity
android:name="example.package.main.activity.MainActivity"
android:label="@string/app_name"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- [START gcm_receiver] -->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="example.package" />
</intent-filter>
</receiver>
<!-- [END gcm_receiver] -->
<!-- [START gcm_listener] -->
<service
android:name="example.package.service.MyGcmListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<!-- [END gcm_listener] -->
<!-- [START instanceId_listener] -->
<service
android:name="example.package.service.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<!-- [END instanceId_listener] -->
<service
android:name="example.package.service.RegistrationIntentService"
android:exported="false"></service>
</application>
</manifest>
以下是我发送给GCM的示例json:
{
"to" : "<snipped>",
"notification" : {
"title" : "<snipped>",
"body" : "<snipped>",
"icon" : "ic_launcher"
},
"data":{"a":"b"},
"content_available": true,
"click_action":"android.intent.action.MAIN"
}
当我点击通知窗口中的项目时,我获得详细登录的唯一方法是:
08-30 11:54:35.942 3684-3980/? D/InputReader: Input event(4): value=1 when=7306609326000
08-30 11:54:35.942 3684-3980/? D/InputReader: Input event(4): value=1 when=7306609326000
08-30 11:54:35.942 3684-3980/? I/InputReader: Touch event's action is 0x0 (deviceType=0) [pCnt=1, s=0.323 ] when=7306609326000
08-30 11:54:35.952 3684-3980/? D/InputReader: lastThreadEndTime = 7303509457022, currentThreadStartTime = 7303509466814, diff = 0
08-30 11:54:35.952 3684-3979/? I/InputDispatcher: Delivering touch to (4091): action: 0x0, toolType: 1
08-30 11:54:35.952 4091-4091/? D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
08-30 11:54:35.952 3684-4035/? D/lights: button : 1 +
08-30 11:54:35.992 3684-4035/? D/lights: button : 1 -
08-30 11:54:36.112 3684-3980/? D/InputReader: Input event(4): value=0 when=7306778958000
08-30 11:54:36.112 3684-3980/? D/InputReader: Input event(4): value=0 when=7306778958000
08-30 11:54:36.112 3684-3980/? I/InputReader: Touch event's action is 0x1 (deviceType=0) [pCnt=1, s=] when=7306778958000
08-30 11:54:36.112 3684-3979/? I/InputDispatcher: Delivering touch to (4091): action: 0x1, toolType: 1
这是在Marshmallow和JellyBean设备上发生的,尽管我之间没有测试任何东西。
我知道我可以单独发送'data'元素,即使在后台也可以启动我的服务,但是如果应用程序已被强制停止,则有不起作用的缺点。我真的想弄清楚我需要做些什么才能获得Google在该链接上描述的行为。
这是MyGcmListenerService - 当应用程序位于前台时,它会被正确调用。
public class MyGcmListenerService extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
/**
* Called when message is received.
*
* @param from SenderID of the sender.
* @param data Data bundle containing message data as key/value pairs.
* For Set of keys use data.keySet().
*/
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
// String message = data.getString("message");
String message = data.getBundle("notification").getString("body");
String customFields = data.getBundle("notification").getString("customFields");
String title = data.getBundle("notification").getString("title");
Log.d("From: " + from);
Log.d( "Title: " + title);
Log.d( "Message: " + message);
Log.d( "CustomFields: " + customFields);
if (from.startsWith("/topics/")) {
// message received from some topic.
} else {
// normal downstream message.
}
// [START_EXCLUDE]
/**
* Production applications would usually process the message here.
* Eg: - Syncing with server.
* - Store message in local database.
* - Update UI.
*/
/**
* In some cases it may be useful to show a notification indicating to the user
* that a message was received.
*/
sendNotification(message);
// [END_EXCLUDE]
}
// [END receive_message]
/**
* Create and show a simple notification containing the received GCM message.
*
* @param message GCM message received.
*/
private void sendNotification(String message) {
Intent intent = new Intent(this, MainActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
/* resumes the current activity - just like pressing link on home page */
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle("GCM Message")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
为了澄清,我正在专门研究如何使用GCM来解决这个问题:
根据https://developers.google.com/cloud-messaging/downstream'当您的应用处于后台时,Android会将带有通知的消息定向到系统托盘。用户点击通知默认情况下会打开应用启动器。'
我已经能够用FCM重写它了,我没有看到这个问题,但我真的想知道我错过了什么。
答案 0 :(得分:0)
基于此related SO question,在JSON中,“通知”用于发送简单通知而无需操作。
通知为开发人员提供了一种简单的方法,可以使用一些预定义的键和可选的自定义键/值对发送用户可见的显示消息。数据有效负载仅包括开发人员的自定义键/值对,客户端应用程序必须处理该消息。您可以发送包含通知和数据有效负载的消息。
此SO question还建议使用条目活动替换消息接收器中的MainActivity.class。
is-visible pulsing
您还可以使用示例代码查看此link,了解如何在点击通知时打开应用程序。希望这有帮助!