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

问题描述 投票:1回答:1

我正在尝试使用GCM作为网关使用PubNub推送通知。通知在操作系统版本<5.0的Android设备上完美传递但是5.0设备从不调用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中有'classpath'com.google.gms:google-services:1.3.0-beta1'

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

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

push-notification google-cloud-messaging android-5.0-lollipop pubnub
1个回答
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" />
© www.soinside.com 2019 - 2024. All rights reserved.