Push notification PubNub GCM通知不适用于Android 5.0及以上版本
我正在尝试使用Push notification PubNub GCM通知不适用于Android 5.0及以上版本,push-notification,google-cloud-messaging,android-5.0-lollipop,pubnub,Push Notification,Google Cloud Messaging,Android 5.0 Lollipop,Pubnub,我正在尝试使用publinub作为网关使用GCM推送通知。 通知在操作系统版本
publinub
作为网关使用GCM
推送通知。
通知在操作系统版本<5.0的android设备上完美传递,但在5.0设备上,GCM
侦听器服务从未被调用(在WiFi和蜂窝网络上),此外,我没有收到任何服务\u not \u AVAILABLE
错误,并且根据输出,注册令牌成功地传递到PubNub
public class RegistrationIntentService extends IntentService {
public RegistrationIntentService() {
super("");
}
@Override
protected void onHandleIntent(Intent intent) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
try {
// In the (unlikely) event that multiple refresh operations occur simultaneously,
// ensure that they are processed sequentially.
synchronized ("Reg Service") {
try {
PNConfiguration.registration_Token = getRegistrationToken();
Log.d("Token",PNConfiguration.registration_Token);
} catch (IOException e) {
e.printStackTrace();
}
sendRegistrationToServer();
sharedPreferences.edit().putBoolean(PNConfiguration.SENT_TOKEN_TO_SERVER, true).apply();
// [END register_for_gcm]
}
} catch (Exception e) {
Log.d("Reg Service", "Failed to complete token refresh", e);
// If an exception happens while fetching the new token or updating our registration data
// on a third-party server, this ensures that we'll attempt the update at a later time.
sharedPreferences.edit().putBoolean(PNConfiguration.SENT_TOKEN_TO_SERVER, false).apply();
}
// Notify UI that registration has completed, so the progress indicator can be hidden.
Intent registrationComplete = new Intent(PNConfiguration.REGISTRATION_COMPLETE);
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
public String getRegistrationToken() throws IOException {
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(PNConfiguration.project_sender_id,
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
return token;
}
private void sendRegistrationToServer() {
Log.d("Came here",PNConfiguration.registration_Token);
Log.d("channel",PNConfiguration.user_channel_name);
PNConfiguration.PUBNUB.enablePushNotificationsOnChannel(PNConfiguration.user_channel_name, PNConfiguration.registration_Token, new
Callback() {
@Override
public void successCallback(String channel, Object message) {
super.successCallback(channel, message);
Log.d("Sent token to Pubnub", PNConfiguration.registration_Token);
}
@Override
public void errorCallback(String channel, PubnubError error) {
super.errorCallback(channel, error);
Log.d("Unsucessful Send token ", error.toString());
}
});
}
}
我的舱单
<service android:name=".GCMPushNotifications.RegistrationIntentService"
android:exported="false">
</service>
<service android:name=".GCMPushNotifications.GCMListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service android:name=".GCMPushNotifications.PNInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter></service>
<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" />
<category android:name="gcm.play.android.samples.com.gcmquickstart" />
</intent-filter>
</receiver>
在我的应用程序级别gradle中
在我的主要活动的创建中
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context);
boolean sentToken = sharedPreferences
.getBoolean(PNConfiguration.SENT_TOKEN_TO_SERVER, false);
if (sentToken) {
Log.d("Info sent","");
} else {
Log.d("error sending token","");
}
}
};
Log.d("value of play services",checkPlayServices()+"");
if (checkPlayServices()) {
// Start IntentService to register this application with GCM.
Log.d("ABC","Starting service");
Intent intent = new Intent(this, RegistrationIntentService.class);
intent.setPackage("com.livongo.lvmobility.GCMPushNotifications.RegistrationIntentService");
startService(intent);
}
如果我在这里遗漏了什么,请帮助我理解。非常感谢任何帮助PubNub实际上不会发送推送通知。当您通过发布向频道发送推送通知时,我们会将推送转发给相应的推送供应商(我们是网关) 这听起来似乎与安卓5.0有更多的关系 您在清单文件中是否具有适当的权限:
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="com.google.android.gcm.demo.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission
android:name="com.google.android.gcm.demo.permission.C2D_MESSAGE" />
PubNub实际上并不传递推送通知。当您通过发布向频道发送推送通知时,我们会将推送转发给相应的推送供应商(我们是网关) 这听起来似乎与安卓5.0有更多的关系 您在清单文件中是否具有适当的权限:
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="com.google.android.gcm.demo.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission
android:name="com.google.android.gcm.demo.permission.C2D_MESSAGE" />
只是想补充一点,毕竟这不是操作系统的问题,当我试图从另一台设备发送消息时,GCMlistenerService从未在我的设备上启动过,但我使用了一个插件(postman)检查我的GCM API并从那里发送消息启动了服务,从那以后(因为该服务已经一劳永逸地启动了)通知正在无缝地工作。我仍然不知道为什么会有这种不可靠性。我只是想补充一点,毕竟这不是操作系统的问题,当我试图从另一台设备发送消息时,GCMlistenerService从未在我的设备上启动,但我使用了一个插件(邮递员)来检查我的GCM API,并从那里发送消息启动了服务,从那时起(因为服务已经启动了一次又一次)通知可以无缝工作。我仍然不确定为什么会有这种不可靠的情况。