PubNub GCM通知无法在Android 5.0及更高版本上运行

时间:2015-07-29 19:22:17

标签: push-notification google-cloud-messaging android-5.0-lollipop pubnub

我正在尝试使用GCM作为网关使用PubNub推送通知。 在具有OS版本<的Android设备上完美地传递通知。 5.0但是使用5.0设备时,永远不会调用GCM侦听器服务(在WiFi和蜂窝电话上),我也没有收到任何SERVICE_NOT_AVAILABLE错误,并且根据输出,注册令牌成功传递给{{ 1}}。

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());

                    }
                });
    }
}

我有' classpath' com.google.gms:google-services:1.3.0-beta1'在我的顶级gradle和

<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。

在我的mainActivity的onCreate里面

apply plugin: 'com.google.gms.google-services'
compile 'com.google.android.gms:play-services:7.5.+'

如果我在这里遗失了什么,请帮助我理解。感谢任何帮助

1 个答案:

答案 0 :(得分:0)

PubNub实际上并不提供推送通知。当您通过发布向频道发送推送通知时,我们会将该推送转发给相应的推送供应商(我们是网关)。

这听起来更像是与android 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" />