Android GCM“背景”通知在点击时不会启动我的应用程序
当我收到'通知'GCM消息时,android会自动从标题和正文中创建一条通知,并将其放入通知栏。Android GCM“背景”通知在点击时不会启动我的应用程序
当我说“通知”在这里,我的意思是本文档中的一个叫通知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>
下面是一个简单的JSON我送到GCM:
{
"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
这是发生在棉花糖和软糖装置,虽然我没有te在两者之间进行任何事
我知道我可以单独发送'data'元素,即使在后台也可以启动我的服务,但是如果应用程序已被强制停止,那么它的缺点是无法工作。我真的想弄清楚我需要做些什么来获得谷歌在该链接上描述的行为。
这里是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重新编写它,但我没有看到这个问题,但我真的想知道我错过了什么。
基于此related SO question,在JSON中,“通知”用于发送无操作的简单通知。
根据该documentation:
通知提供供开发者发送一个用户可见的显示消息用一些预定义的键和可选的自定义键/值对的简单方法。数据有效载荷仅包含开发人员的自定义键/值对,而客户端应用程序必须处理该消息。您可以发送既有通知又有数据有效载荷的消息。
这个SO question也建议用消息接收器中的MainActivity.class替换入口活动。
final Intent notificationIntent = new Intent(context, MainActivity.class); notificationIntent.setAction(Intent.ACTION_MAIN); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
您还可以检查此link关于如何被点击通知时,打开应用程序的示例代码。希望这可以帮助!
感谢您的信息,但它不应该是没有行动,它应该打开发射器的意图。你的第二部分显示了java代码,看起来像你在推荐我使用“数据”消息,这正是我试图避免的。根据https://developers.google.com/cloud-messaging/downstream'当您的应用程序处于后台时,Android会将消息通知系统托盘。用户点击通知会默认打开应用启动器 –
MyGcmListenerService.java的邮编 – Bhupendra
@Bhupendra我会发布它,但它与googles git页面上的gcm相同。 在此过程中,当通知栏中的通知由操作系统创建时,它不会以任何方式调用。 –