【问题标题】:apache cordova app crashes after receiving parse.com push notificationapache cordova 应用程序在收到 parse.com 推送通知后崩溃
【发布时间】:2014-06-26 22:41:59
【问题描述】:

我想将 parse.com 推送通知服务添加到我的 Cordova 应用程序。我已阅读 parse.com 快速入门指南,并将 SDK 文件放在 libs 文件夹中。之后,我将代码添加到主 java 文件和 AndroidManifest.xml 中。该应用程序正常工作。 parse.com 可以接收我的设备令牌,我可以向设备发送推送通知,但是在两三个推送通知之后,应用程序将崩溃。

catlog 中的错误信息是这样的:

05-07 02:08:25.185: E/com.parse.PushService(28735): 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.
05-07 02:08:25.185: D/AndroidRuntime(28735): Shutting down VM
05-07 02:08:25.185: W/dalvikvm(28735): threadid=1: thread exiting with uncaught exception (group=0x41b0de48)
05-07 02:08:25.185: E/AndroidRuntime(28735): FATAL EXCEPTION: main
05-07 02:08:25.185: E/AndroidRuntime(28735): Process: ir.wedesign.pushapp, PID: 28735
05-07 02:08:25.185: E/AndroidRuntime(28735): java.lang.RuntimeException: Unable to start service com.parse.PushService@42b596a8 with Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10 pkg=ir.wedesign.pushapp cmp=ir.wedesign.pushapp/com.parse.PushService (has extras) }: java.lang.RuntimeException: applicationContext is null. You must call Parse.initialize(context, applicationId, clientKey) before using the Parse library.
05-07 02:08:25.185: E/AndroidRuntime(28735):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2726)
05-07 02:08:25.185: E/AndroidRuntime(28735):    at android.app.ActivityThread.access$2100(ActivityThread.java:139)
05-07 02:08:25.185: E/AndroidRuntime(28735):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1297)
05-07 02:08:25.185: E/AndroidRuntime(28735):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-07 02:08:25.185: E/AndroidRuntime(28735):    at android.os.Looper.loop(Looper.java:136)
05-07 02:08:25.185: E/AndroidRuntime(28735):    at android.app.ActivityThread.main(ActivityThread.java:5105)
05-07 02:08:25.185: E/AndroidRuntime(28735):    at java.lang.reflect.Method.invokeNative(Native Method)
05-07 02:08:25.185: E/AndroidRuntime(28735):    at java.lang.reflect.Method.invoke(Method.java:515)
05-07 02:08:25.185: E/AndroidRuntime(28735):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
05-07 02:08:25.185: E/AndroidRuntime(28735):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
05-07 02:08:25.185: E/AndroidRuntime(28735):    at dalvik.system.NativeStart.main(Native Method)
05-07 02:08:25.185: E/AndroidRuntime(28735): Caused by: java.lang.RuntimeException: applicationContext is null. You must call Parse.initialize(context, applicationId, clientKey) before using the Parse library.
05-07 02:08:25.185: E/AndroidRuntime(28735):    at com.parse.Parse.checkContext(Parse.java:557)
05-07 02:08:25.185: E/AndroidRuntime(28735):    at com.parse.Parse.getApplicationContext(Parse.java:165)
05-07 02:08:25.185: E/AndroidRuntime(28735):    at com.parse.ManifestInfo.getContext(ManifestInfo.java:221)
05-07 02:08:25.185: E/AndroidRuntime(28735):    at com.parse.ManifestInfo.getPackageManager(ManifestInfo.java:229)
05-07 02:08:25.185: E/AndroidRuntime(28735):    at com.parse.ManifestInfo.getPackageInfo(ManifestInfo.java:240)
05-07 02:08:25.185: E/AndroidRuntime(28735):    at com.parse.ManifestInfo.deviceSupportsGcm(ManifestInfo.java:325)
05-07 02:08:25.185: E/AndroidRuntime(28735):    at com.parse.ManifestInfo.getPushType(ManifestInfo.java:109)
05-07 02:08:25.185: E/AndroidRuntime(28735):    at com.parse.PushService.onStartCommand(PushService.java:377)
05-07 02:08:25.185: E/AndroidRuntime(28735):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2709)
05-07 02:08:25.185: E/AndroidRuntime(28735):    ... 10 more

这是我的主要 java 文件:

/*
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
 */

package ir.wedesign.pushapp;

import android.os.Bundle;
import org.apache.cordova.*;
import com.parse.Parse;
import com.parse.ParseAnalytics;
import com.parse.ParseInstallation;
import com.parse.PushService;

public class PushApp extends CordovaActivity 
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.init();
        // Set by <content src="index.html" /> in config.xml
        super.loadUrl(Config.getStartUrl());
        //super.loadUrl("file:///android_asset/www/index.html");
        Parse.initialize(this, "xJK9ZA0Dtsjd8RwoNUUsIHPXzMRbmcxXGWqiw", "19g9eUKjLjlUdogHEXStUABNcvy2rA6azb5mXy");
        PushService.setDefaultPushCallback(this, PushApp.class);
        ParseInstallation.getCurrentInstallation().saveInBackground();

    }
}

我的 manifest.xml 文件是:

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" android:windowSoftInputMode="adjustPan" package="ir.wedesign.pushapp" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <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" />
    <permission android:protectionLevel="signature"
        android:name="ir.wedesign.pushapp.permission.C2D_MESSAGE" />
    <uses-permission android:name="ir.wedesign.pushapp.permission.C2D_MESSAGE" />

    <application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">   
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:name="PushApp" android:theme="@android:style/Theme.Black.NoTitleBar">
            <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.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="ir.wedesign.pushapp" />
  </intent-filter>
</receiver>
    </application>
    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />
</manifest>

我搜索过,似乎许多开发人员都遇到了完全相同的问题。我在 parse.com 帮助论坛中找到了两个主题: https://www.parse.com/questions/app-crashes-after-after-screen-unlock https://www.parse.com/questions/cannot-send-push-to-android-after-app-is-closed-until-screen-unlock 这个在这里: Android App crashes when push is received and app is closed

但是对于不懂 java 的人来说,解决方案并不清楚。如何更改我的 AndroidManifest.xml 和主 java 文件以使其正常工作。

提前致谢

【问题讨论】:

    标签: android cordova push-notification parse-platform


    【解决方案1】:

    如果您将 &lt;application&gt; 命名为 android:name="MainApplication",请检查您的 manifest.xml

    &lt;activity&gt; 作为android:name="PushApp"

    【讨论】:

      【解决方案2】:

      Parse 推送服务无法启动,因为 Parse.initialize 尚未被调用。 如果您从 Activity 的 onCreate 调用 Parse.initialize,则该调用应该在 Application.onCreate 中。 确保您的 Application 类已在您的 AndroidManifest.xml 中使用您的标记的 android:name 属性注册。

      尝试将 Activity 和 Application 分开,创建一个新的类文件,例如主应用程序。

      PushApp.java:

      public class PushApp extends CordovaActivity {
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              super.init();
              // Set by <content src="index.html" /> in config.xml
              super.loadUrl(Config.getStartUrl());
              //super.loadUrl("file:///android_asset/www/index.html");
          }
      }
      

      MainApplication.java:

      public class MainApplication extends Application {
          @Override
          public void onCreate() {
              super.onCreate();
              Parse.initialize(this, "xJK9ZA0Dtsjd8RwoNUUsIHPXzMRbmcxXGWqiw", "19g9eUKjLjlUdogHEXStUABNcvy2rA6azb5mXy");
              PushService.setDefaultPushCallback(this, PushApp.class);
              ParseInstallation.getCurrentInstallation().saveInBackground();
          }
      }
      

      并改变:

      <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:name="PushApp" android:theme="@android:style/Theme.Black.NoTitleBar">
      

      到:

      <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:name="MainApplication" android:theme="@android:style/Theme.Black.NoTitleBar">
      

      现在您还应该在应用关闭/屏幕锁定时收到推送通知

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-04
        • 1970-01-01
        • 1970-01-01
        • 2015-09-21
        相关资源
        最近更新 更多