1

我已经按照以下所有说明在 Android 中为应用程序配置了推送通知: https ://github.com/ParsePlatform/Parse-Server/wiki/Push-Configuring-Clients https://parseplatform.github.io/docs/安卓/指南/#push-notifications

但无论我做什么,我使用的手机似乎都无法收到任何推送通知。

相同的应用程序被配置为在 iOS 上接收通知并且运行良好。

这些是清单中的相关位:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxy.xxy">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<permission
    android:name="com.xxy.xxy.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.xxy.xxy.permission.C2D_MESSAGE" />


<application
    android:name="com.xxy.xxy.MiXXYApplication"
    android:allowBackup="true"
    android:icon="@drawable/logoapp"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

   <service android:name="com.parse.PushService" />

    <receiver
        android:name="com.parse.ParsePushBroadcastReceiver"
        android:exported="false">
        <intent-filter>
            <action android:name="com.parse.push.intent.RECEIVE" />
            <action android:name="com.parse.push.intent.DELETE" />
            <action android:name="com.parse.push.intent.OPEN" />
        </intent-filter>
    </receiver>
    <receiver
        android:name="com.parse.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="com.xxy.xxy" />
        </intent-filter>
    </receiver>

    <meta-data
        android:name="com.parse.push.gcm_sender_id"
        android:value="id:123456789012" />
</application>

这是 Application 类(将 AppID 和 Client Key 替换为 Back4App 给出的当前值):

public class MiXXYApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        Parse.setLogLevel(Parse.LOG_LEVEL_VERBOSE);
        Parse.initialize(new Parse.Configuration.Builder(getApplicationContext())
                .applicationId("back4App_AppID")
                .clientKey("back4App_ClientKey")
                .server("https://parseapi.back4app.com/")
                .build()
        );

    }
}

这些是我调用来注册安装的方法(我不能在 Application 类上这样做,因为我需要 userId,登录后)

public void enablePush(final String userId) {
    ParseInstallation installation = ParseInstallation.getCurrentInstallation();
    installation.put("GCMSenderId", "123456789012");
    installation.put("userId", userId);
    installation.saveInBackground(new SaveCallback() {
        @Override
        public void done(ParseException e) {
            if (e == null) {
                Log.i("push", "ok");
                subscribe("");
                subscribe(userId);
            } else {
                Log.i("push", "nok");
                e.printStackTrace();
            }
        }
    });
}


public void subscribe(String channel){
    ParsePush.subscribeInBackground(channel, new SaveCallback() {
        @Override
        public void done(ParseException e) {
            if (e == null) {
                Log.i("Push", "subscribed");
            } else {
                Log.i("push", "nok");
                e.printStackTrace();
            }
        }
    });
}

这是 Parse DB 上的安装注册表:

ObjectID: l1tkO3YM2r
GCMSenderID: 123456789012
deviceToken: APA91bHeKnj...
localeIdentifier: en-US
badge: (undefined)
parseVersion: 1.13.1
ACL: Public Read + Write
appIdentifier: com.xxy.xxy
appName: appName
deviceType: android
channels: ["","CyTPw5xc4r"]
pushType: gcm
installationId: 8cf9c606-5a0e...
userId: CyTPw5xc4r

在谷歌开发者控制台中,项目编号为 123456789012,我创建了一个 API 密钥,并在 Back4App 仪表板的 Android 推送通知设置中添加了 API 密钥 + 项目编号。

详细模式下的 logcat 显示如下:

09-21 09:29:19.863 10721-10721/com.xxy.xxy V/com.parse.ManifestInfo: Using gcm for push.
09-21 09:29:19.884 10721-10885/com.xxy.xxy V/com.parse.CachedCurrentInstallationController: Successfully deserialized Installation object
09-21 09:29:19.899 10721-10886/com.xxy.xxy V/com.parse.GcmRegistrar: Sending GCM registration intent
09-21 09:29:29.782 10721-11340/com.xxy.xxy V/com.parse.GcmRegistrar: Received deviceToken <APA91bHeKnj...> from GCM.

据我所知,一切都很好,但我的手机没有收到任何通知。

我的设置有什么问题吗?谢谢!

4

1 回答 1

1

解决了!

问题是,您应该从https://developers.google.com/mobile/add获取它,而不是像所有指南所说的那样获取 Api 密钥。

它生成了另一个 API 密钥并激活了服务,然后它就可以工作了。

于 2016-09-27T16:13:35.157 回答