应用程序解除后,解析推送通知会崩溃

时间:2016-06-24 21:56:41

标签: android parse-platform push-notification android-notifications

是的,我知道Parse将在明年关闭,但由于某种原因,我现在必须使用它的功能。发生了什么,推送通知部分在应用程序处于活动状态时工作正常,或者甚至在后台工作正常,但是如果我解雇应用程序,这意味着他不再运行,它会在我崩溃时崩溃应用程序收到推送通知。我经常搜索,所有人都告诉我把Parse.initialize(...)放在Application而不是Activity上,实际上我正在做什么。关于我的项目有什么问题的任何线索?

这是我的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.package.parse" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<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_TASKS" />

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

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


<application
    android:name=".app.MyApp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:replace="android:icon">
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/google_maps_key" />
    <meta-data
        android:name="com.google.android.urlshortener.API_KEY"
        android:value="@string/google_maps_key"/>
    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/fb_app_id" />

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

    <provider android:authorities="com.facebook.app.FacebookContentProvider16..."
        android:name="com.facebook.FacebookContentProvider"
        android:exported="true"/>

    <receiver
        android:name=".receiver.CustomPushReceiver"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.USER_PRESENT" />
            <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.package.parse" />
        </intent-filter>
    </receiver>

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

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

    <activity
        android:name=".ui.Home"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar" >
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />

        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

这是我的申请:

public class MyApp  extends Application {

private static MyApp instance;
public static PreferencesManager prefs;

@Override
public void onCreate() {
    super.onCreate();
    instance = this;

 PreferencesManager.initializeInstance(context);
    prefs = PreferencesManager.getInstance();

    if(isLogged()) {
        doParseStuff();
    }
}

public void configureParse(){


    Parse.initialize(this, AppConfig.PARSE_APPLICATION_ID, AppConfig.PARSE_CLIENT_KEY);
    ParseInstallation installation = ParseInstallation.getCurrentInstallation();
    installation.put("GCMSenderId", this.getString(R.string.google_app_id));
    installation.saveInBackground();

    registerParse();
    ParseInstallation.getCurrentInstallation().saveInBackground();
}

public static void registerParse() {
    final String channel = MyApp.getCurrentUser() != null &&
            !TextUtils.isEmpty(MyApp.getCurrentUser().getUsername()) ? MyApp.getCurrentUser().getUsername() : "";

    if(!TextUtils.isEmpty(channel) ){
        ParsePush.subscribeInBackground(channel, new SaveCallback() {
            @Override
            public void done(ParseException e) {
                    MyApp.setParseConfigured(true);

            }
        });
    }

}

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);

    MultiDex.install(this);

}

public static boolean isLogged() {
    return prefs.getBoolean(Constants.SharedPreferences.IS_LOGGED);
}

public static void setLogged(boolean b) {
    prefs.setValue(Constants.SharedPreferences.IS_LOGGED, b);
}

public static boolean isParseConfigured() {
    return prefs.getBoolean(Constants.SharedPreferences.IS_PARSE_CONFIGURED);
}

public static void setParseConfigured(boolean b) {
    prefs.setValue(Constants.SharedPreferences.IS_PARSE_CONFIGURED, b);
}

public static void setCurrentUser(CurrentUser user){

    prefs.setValue(Constants.SharedPreferences.USER, JSONUtils.getJSONString(user));

    instance.doParseStuff();
}

private void doParseStuff(){

        if(!isParseConfigured()) {
            configureParse();
        }
}

public static CurrentUser getCurrentUser(){
    return (CurrentUser) JSONUtils.getJSONObject(prefs.getString(Constants.SharedPreferences.USER), CurrentUser.class);
}

}

这是我的CustomPushReceiver:

public class CustomPushReceiver extends ParsePushBroadcastReceiver {

private NotificationUtils notificationUtils;

private Intent parseIntent;

public CustomPushReceiver() {
    super();
}

@Override
protected void onPushReceive(Context context, Intent intent) {
    //   super.onPushReceive(context, intent);

    if (intent == null)
        return;

    String data = intent.getExtras().getString("com.parse.Data");

        parseIntent = intent;

        parsePushJson(context, notificationModel.getAlert());
}

@Override
protected void onPushDismiss(Context context, Intent intent) {
    super.onPushDismiss(context, intent);
}

@Override
protected void onPushOpen(Context context, Intent intent) {
    super.onPushOpen(context, intent);
}


private void parsePushJson(Context context, String alert) {

    Intent resultIntent = new Intent(context, Home.class);
    showNotificationMessage(context, "", alert, resultIntent);

}


private void showNotificationMessage(Context context, String title, String message, Intent intent) {

    notificationUtils = new NotificationUtils(context);

    intent.putExtras(parseIntent.getExtras());

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    notificationUtils.showNotificationMessage(title, message, intent);

    }
}

最后一个错误日志:

java.lang.RuntimeException: Unable to create service com.parse.PushService: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context com.parse.ParsePlugins$Android.applicationContext()' on a null object reference
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2905)
at android.app.ActivityThread.-wrap4(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1437)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context com.parse.ParsePlugins$Android.applicationContext()' on a null object reference
at com.parse.PushService.onCreate(PushService.java:230)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2895)
at android.app.ActivityThread.-wrap4(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1437) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5443) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

编辑1

这是de PushService内的代码,第230行:

if (ParsePlugins.Android.get().applicationContext() == null) {
  PLog.e(TAG, "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.");
  stopSelf();
  return;
}

1 个答案:

答案 0 :(得分:0)

它似乎只是在最后发布的解析lib版本中引起的,所以我从compile 'com.parse:parse-android:1.+'降级为编译'com.parse:parse-android:1.10.1',现在它已经解决了。另外,我必须删除installation.put("GCMSenderId", this.getString(R.string.google_app_id));部分才能在应用程序停止时收到通知。