【发布时间】:2016-08-18 06:44:11
【问题描述】:
我正在尝试向我的 Android 应用程序添加推送通知,但它似乎不起作用。这是我能做的:
我的客户端应用程序成功接收到它的
registrationId。我的服务器成功接收到它的身份验证令牌。
服务器使用其身份验证令牌和设备的
registrationId发送消息。从服务器向客户端发送消息时,我收到一条成功的消息响应代码(例如
id=0:1335303367614556%fd55792500000030响应代码:200,根据 Google 关于 C2DM 的文档,这应该是一条成功消息)。
但是我的设备上没有任何显示,而是每当我发送推送通知时,我的 logcat 中都会出现错误,它们是:
08-18 11:17:53.824 11070-11070/example.haris.com.examplelayout E/GcmReceiver: Failed to resolve target intent service, skipping classname enforcement 08-18 11:17:53.825 11070-11070/example.haris.com.examplelayout E/GcmReceiver: Error while delivering the message: ServiceIntent not found. 08-18 11:17:53.829 11070-11070/example.haris.com.examplelayout E/FirebaseInstanceId: Failed to resolve target intent service, skipping classname enforcement 08-18 11:17:53.830 11070-11070/example.haris.com.examplelayout E/FirebaseInstanceId: Error while delivering the message: ServiceIntent not found.
这是我用来实现上述功能的 3 个类:
GCMPushReceiverService.java
http://i.stack.imgur.com/twLKF.png
GCMRegistrationIntentService
http://i.stack.imgur.com/DfTbm.png
GCMTokenRefreshListenerService.java
public class GCMTokenRefreshListenerService extends InstanceIDListenerService {
/**
* When token refresh, start service to get new token
*/
@Override
public void onTokenRefresh() {
Intent intent = new Intent(this, GCMRegistrationIntentService.class);
startService(intent);
}
}
这是我使用 BroadcastReciever 的主要活动代码:
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//Check type of intent filter
if(intent.getAction().equals(GCMRegistrationIntentService.REGISTRATION_SUCCESS)){
//Registration success
String token = intent.getStringExtra("token");
Toast.makeText(getApplicationContext(), "GCM token:" + token, Toast.LENGTH_LONG).show();
} else if(intent.getAction().equals(GCMRegistrationIntentService.REGISTRATION_ERROR)){
//Registration error
Toast.makeText(getApplicationContext(), "GCM registration error!!!", Toast.LENGTH_LONG).show();
} else {
//Tobe define
}
}
};
//Check status of Google play service in device
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if(ConnectionResult.SUCCESS != resultCode) {
//Check type of error
if(GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
Toast.makeText(getApplicationContext(), "Google Play Service is not install/enabled in this device!", Toast.LENGTH_LONG).show();
//So notification
GooglePlayServicesUtil.showErrorNotification(resultCode, getApplicationContext());
} else {
Toast.makeText(getApplicationContext(), "This device does not support for Google Play Service!", Toast.LENGTH_LONG).show();
}
} else {
//Start service
Intent itent = new Intent(this, GCMRegistrationIntentService.class);
startService(itent);
}
这是我的 AndroidManifest.xml:
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="example.haris.com.examplelayout.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission
android:name="example.haris.com.examplelayout.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/logo"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Login.MainScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="example.haris.com.examplelayout" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
</intent-filter>
</receiver>
<service android:name=".GCMPushReceiverService" android:exported="false">
<intent-filter android:priority="1000">
<action android:name="com.google.android.c2dm.intent.RECIEVE"></action>
</intent-filter>
</service>
<service android:name=".GCMTokenRefreshListenerService" android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.idd.InstanceID"></action>
</intent-filter>
</service>
<service
android:name=".GCMRegistrationIntentService"
android:exported="false">
</service>
编辑:
更新清单
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="example.haris.com.examplelayout" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
</intent-filter>
</receiver>
<service android:name="example.haris.com.examplelayout.GCMPushReceiverService" android:exported="false">
<intent-filter android:priority="1000">
<action android:name="com.google.android.c2dm.intent.RECIEVE"></action>
</intent-filter>
</service>
<service android:name="example.haris.com.examplelayout.GCMTokenRefreshListenerService" android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.idd.InstanceID"></action>
</intent-filter>
</service>
<service
android:name="example.haris.com.examplelayout.GCMRegistrationIntentService"
android:exported="false">
</service>
【问题讨论】:
-
onMessageReceived() 是否被调用?
-
我认为是,如何确认?
-
放个log语句就好了。
-
我有,没有显示任何东西!!!
-
您是否在 Manifest 文件中正确声明了应用的包名称?就像在这篇 SO 帖子中一样 - Android GCM server sent but GCM not pushing to device。
标签: android push-notification google-cloud-messaging