【发布时间】:2015-07-29 19:22:17
【问题描述】:
我正在尝试使用PubNub 作为网关来处理GCM 推送通知。
通知在操作系统版本 GCM 侦听器服务(在 WiFi 和蜂窝网络上),我也没有收到任何 SERVICE_NOT_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 和
apply plugin: 'com.google.gms.google-services'
compile 'com.google.android.gms:play-services:7.5.+'
在我的应用级别 gradle 中。
在我的 mainActivity 的 onCreate 中
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);
}
如果我在这里遗漏了什么,请帮助我理解。感谢任何帮助
【问题讨论】:
-
只是想补充一下答案,毕竟这不是操作系统问题,当我尝试从另一台设备发送消息时,GCMlistenerService 从未在我的设备上启动,但我使用了插件(邮递员)检查我的 GCM API 并从那里发送消息启动服务,从那时起(因为服务已经一劳永逸地启动)通知无缝工作。我仍然不确定为什么会出现这种不可靠性。
标签: push-notification google-cloud-messaging android-5.0-lollipop pubnub