编程语言
首页 > 编程语言> > java-在应用未运行时处理解析推送通知(Android)

java-在应用未运行时处理解析推送通知(Android)

作者:互联网

解析版本:1.9.2

当我从Parse仪表板发送推送通知并且应用未运行时,出现以下对话框错误:

“很遗憾,“应用名称”已停止.”

通知在应用程序运行时起作用,但是很明显,推送通知实用程序的绝大部分是在未收到通知时接收通知.

我已经按照每个集成教程进行了准备:https://www.parse.com/tutorials/android-push-notifications

AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.elgami.customizer"
    android:versionCode="23"
    android:versionName="2.2" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <uses-feature android:name="android.hardware.camera" />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <!--
      IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
      to match your app's package name + ".permission.C2D_MESSAGE".
    -->

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

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

    <application
        android:name="com.elgami.application.GlobalApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <!-- Google Analytics Version v4 needs this value for easy tracking -->
        <!--
            <meta-data android:name="com.google.android.gms.analytics.globalConfigResource"
            android:resource="@xml/global_tracker" />
        -->

        <!--
            <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        -->

        <meta-data android:name="com.elgami.customizer.notification_icon" android:resource="@drawable/ic_launcher"/>

        <activity
            android:name=".IntroActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".PurchaseActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name="com.elgami.gallery.GalleryActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
        </activity>

        <service
            android:name="com.paypal.android.sdk.payments.PayPalService"
            android:exported="false" />

        <activity android:name="com.paypal.android.sdk.payments.PaymentActivity" />
        <activity android:name="com.paypal.android.sdk.payments.LoginActivity" />
        <activity android:name="com.paypal.android.sdk.payments.PaymentMethodActivity" />
        <activity android:name="com.paypal.android.sdk.payments.PaymentConfirmActivity" />
        <activity android:name="com.paypal.android.sdk.payments.PayPalFuturePaymentActivity" />
        <activity android:name="com.paypal.android.sdk.payments.FuturePaymentConsentActivity" />
        <activity android:name="com.paypal.android.sdk.payments.FuturePaymentInfoActivity" />
        <activity android:name="com.paypal.android.sdk.payments.PayPalProfileSharingActivity" />
        <activity android:name="com.paypal.android.sdk.payments.ProfileSharingConsentActivity" />
        <activity
            android:name="io.card.payment.CardIOActivity"
            android:configChanges="keyboardHidden|orientation" />
        <activity android:name="io.card.payment.DataEntryActivity" />

        <provider
            android:name="com.elgami.utility.ElgamiContentProvider"
            android:authorities="com.elgami"
            android:enabled="true"
            android:exported="true" >
        </provider>

        <activity
            android:name=".PaymentProcessingActivity"
            android:label="@string/title_activity_payment_processing" >
        </activity>
        <activity
            android:name=".WebWrapper"
            android:label="@string/title_activity_web_view" >
        </activity>
        <activity
            android:name="com.elgami.utility.WrapperWeb"
            android:label="@string/title_activity_wrapper_web" >
        </activity>

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

        <!-- Custom Receiver -->
        <receiver
            android:name="com.elgami.customizer.Receiver"
            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>
        <!-- End Custom Receiver -->

        <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.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.tutorials.pushnotifications" to match your app's package name.
                -->
                <category android:name="com.elgami.customizer" />
            </intent-filter>
        </receiver>

        <!-- replace @drawable/push_icon with your push icon identifier -->
        <meta-data android:name="com.parse.push.notification_icon" android:resource="@drawable/ic_notification"/>

    </application>

</manifest>

初始化我的Parse的Java类.可能会发生什么?

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        Parse.initialize(this, "myAppID", "myClientID");

        ParseInstallation.getCurrentInstallation().saveInBackground();

        ParsePush.subscribeInBackground("", new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (e == null) {
                    Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");
                } else {
                    Log.e("com.parse.push", "failed to subscribe for push", e);
                }
            }
        });
}

日志猫:

06-11 23:44:02.880  12738-12738/com.elgami.customizer E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.elgami.customizer, PID: 12738
    java.lang.RuntimeException: Unable to start service com.parse.PushService@39ffceb5 with Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10 pkg=com.elgami.customizer cmp=com.elgami.customizer/com.parse.PushService (has extras) }: java.lang.RuntimeException: applicationContext is null. You must call Parse.initialize(Context) before using the Parse library.
            at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2911)
            at android.app.ActivityThread.access$2100(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            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:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.RuntimeException: applicationContext is null. You must call Parse.initialize(Context) before using the Parse library.
            at com.parse.Parse.checkContext(Parse.java:448)
            at com.parse.Parse.getApplicationContext(Parse.java:267)
            at com.parse.ManifestInfo.getContext(ManifestInfo.java:324)
            at com.parse.ManifestInfo.getIntentReceivers(ManifestInfo.java:132)
            at com.parse.ManifestInfo.hasIntentReceiver(ManifestInfo.java:124)
            at com.parse.ManifestInfo.getPushUsesBroadcastReceivers(ManifestInfo.java:176)
            at com.parse.PushService.wipeRoutingAndUpgradePushStateIfNeeded(PushService.java:504)
            at com.parse.PushService.onStartCommand(PushService.java:485)
            at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2894)
            at android.app.ActivityThread.access$2100(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            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:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

解决方法:

我认为您必须将Application类中的所有Parse初始化初始化为

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

        Parse.initialize(this, "myAppID", "myClientID");

        ParseInstallation.getCurrentInstallation().saveInBackground();

        ParsePush.subscribeInBackground("", new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (e == null) {
                    Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");
                } else {
                    Log.e("com.parse.push", "failed to subscribe for push", e);
                }
            }
        });
    }


}

查看此链接的custom Application class

标签:parse-platform,java,android
来源: https://codeday.me/bug/20191120/2041919.html