当应用程序关闭时,Android 不会收到来自 Parse Push 的推送通知

标签 android parse-platform push-notification

当我在 Android 手机上测试 Parse Push 时,只要应用程序运行,推送通知就会通过。 如果应用程序关闭:我只会收到一条消息:<App name> has stopped. - 我没有收到任何推送 - 但仍然收到停止通知弹出窗口。我已一步步遵循 Parse 的快速入门指南。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.ostsia.ostsia">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <!--
      IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
      to match your app's package name + ".permission.C2D_MESSAGE".
    -->
    <permission android:protectionLevel="signature"
        android:name="org.ostsia.ostsia.permission.C2D_MESSAGE" />
    <uses-permission android:name="org.ostsia.ostsia.permission.C2D_MESSAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ostsia"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="org.ostsia.ostsia.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name="com.parse.PushService"/>
        <receiver android:name="com.parse.ParseBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>
        <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" />

                <!--
                  IMPORTANT: Change "com.parse.starter" to match your app's package name.
                -->
                <category android:name="org.ostsia.ostsia" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

03-03 21:27:10.829  31149-31149/org.ostsia.ostsia E/com.parse.PushService﹕ The Parse push service cannot start because Parse.initialize has not yet been called. If you call Parse.initialize from an Activity's onCreate, that call should instead be in the Application.onCreate. Be sure your Application class is registered in your AndroidManifest.xml with the android:name property of your <application> tag.
03-03 21:27:10.829  31149-31149/org.ostsia.ostsia D/AndroidRuntime﹕ Shutting down VM
03-03 21:27:10.839  31149-31149/org.ostsia.ostsia E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: org.ostsia.ostsia, PID: 31149
    java.lang.RuntimeException: Unable to start service com.parse.PushService@4d17bf6 with Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10 pkg=org.ostsia.ostsia cmp=org.ostsia.ostsia/com.parse.PushService (has extras) }: java.lang.RuntimeException: applicationContext is null. You must call Parse.initialize(context, applicationId, clientKey) before using the Parse library.
            at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3278)
            at android.app.ActivityThread.access$2200(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1520)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5834)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
     Caused by: java.lang.RuntimeException: applicationContext is null. You must call Parse.initialize(context, applicationId, clientKey) before using the Parse library.
            at com.parse.Parse.checkContext(Parse.java:451)
            at com.parse.Parse.getApplicationContext(Parse.java:219)
            at com.parse.ManifestInfo.getContext(ManifestInfo.java:322)
            at com.parse.ManifestInfo.getPackageName(ManifestInfo.java:326)
            at com.parse.ManifestInfo.getIntentReceivers(ManifestInfo.java:131)
            at com.parse.ManifestInfo.hasIntentReceiver(ManifestInfo.java:123)
            at com.parse.ManifestInfo.getPushUsesBroadcastReceivers(ManifestInfo.java:174)
            at com.parse.PushService.wipeRoutingAndUpgradePushStateIfNeeded(PushService.java:454)
            at com.parse.PushService.onStartCommand(PushService.java:435)
            at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3261)
            at android.app.ActivityThread.access$2200(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1520)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5834)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
03-03 21:27:12.599  31149-31149/org.ostsia.ostsia I/Process﹕ Sending signal. PID: 31149 SIG: 9

最佳答案

在应用程序类中初始化解析,如下所示

public class AppClass extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        // Parse.enableLocalDatastore(this); // if you need this, put before init
        Parse.initialize(this, "gV6QK", "JJmFo99");
        PushService.setDefaultPushCallback(getApplicationContext(), MainActivity.class);
        ParseInstallation.getCurrentInstallation().saveInBackground();
    }
}

不要忘记在 list 中引用您的应用程序类

<application android:name=".AppClass">

从 MainActivity 中删除 Parse init

关于当应用程序关闭时,Android 不会收到来自 Parse Push 的推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28838855/

相关文章:

java - 使用 Parse 从应用程序引擎发送推送通知

javascript - Sendbird、cordova 推送通知功能

android - 定期编辑 SharedPreferences 有什么缺点吗?

android - 当用户在水平回收器 View 上滑动时停止垂直移动

Android Studio list 合并失败 minSdkVersion 错误

ios - 如何将图像保存到 Parse 的主要用户类?

java - 如何将 List<String> 作为 Intent extra 传递?

android - FCM 推送通知问题 : When I tap on notification and every time i got getExtras() from intent is null.(应用程序在后台)

android - admob 可以在 sdk 2.2 上运行吗?无法编译项目

javascript - Parse.com + 云代码 + Android + Stripe