2015-06-12 156 views
3

解析版本:1.9.2手柄解析推送通知(Android)

當我從我的儀表盤解析發送推送通知和應用程序沒有運行,我收到下面的對話框錯誤:

」不幸的是「應用程序名稱」已停止。「

通知在應用程序運行時正常工作,但顯然,推送通知的絕大多數實用程序都是在不接收推送通知時使用的。

我已經按照各自的整合教程的一個發球: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> 

在我的分析是初始化的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); 
       } 
      } 
     }); 
} 

的logcat:

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 [email protected] 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) 
+0

檢查答案我已經發布 –

回答

3

我認爲你必須初始化所有Parse intialization在Application類作爲

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

+1

驚人!這工作正好。問題在於我在我的一個活動中初始化Parse,而不是定製應用程序。我希望我有更多的聲譽,以便我可以給你一些投票! – drearypanoramic

+1

好的,我剛剛做到了。謝謝。 – drearypanoramic