我正在尝试将推送通知添加到我的Android应用程序,但它似乎无法正常工作。这是我能做的:
我的客户端应用程序成功收到其registrationId
。
我的服务器成功收到其身份验证令牌。
服务器使用其身份验证令牌和设备registrationId
发送邮件。
当从服务器向客户端发送消息时,我收到一条成功的消息响应代码(例如id=0:1335303367614556%fd55792500000030
响应代码:200,根据Google的C2DM文档应该是成功的消息)。< / p>
但是我的设备上没有显示任何内容,相反,每当我发送推送通知时,我的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>
答案 0 :(得分:0)
使用完整的包名和子包
更新您的清单文件 <service android:name="example.haris.com.examplelayout.Utility.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.Utility.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.Utility.GCMRegistrationIntentService"
android:exported="false">
</service>
修改
<receiver
android:name=".ExternalReceiver "
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" />
</intent-filter>
</receiver>
创建新班级
public class ExternalReceiver extends BroadcastReceiver {
String TAG = "ExternalReceiver";
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "Got push notification inside onReceive: ");
}
}