【问题标题】:PubNub GCM notifications not working on Android 5.0 and abovePubNub GCM 通知不适用于 Android 5.0 及更高版本
【发布时间】: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


【解决方案1】:

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" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 2015-10-04
    • 2018-01-09
    • 2014-06-14
    • 1970-01-01
    相关资源
    最近更新 更多