关闭应用程序后解析推送通知崩溃

Posted

技术标签:

【中文标题】关闭应用程序后解析推送通知崩溃【英文标题】:Parse push notification crashs after the app is dismissed 【发布时间】:2016-06-24 21:56:41 【问题描述】:

是的,我知道 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;

【问题讨论】:

PushService 中的第 230 行 @ρяσѕρєяK 我刚刚用代码编辑了问题。 丹尼尔的意思是ParsePlugins.Android.get()返回null。改成if)ParsePlugins.Android.get() ! = null) ... your code here 问题是这段代码在解析库里面,我不能改代码。 【参考方案1】:

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

【讨论】:

以上是关于关闭应用程序后解析推送通知崩溃的主要内容,如果未能解决你的问题,请参考以下文章

Android - 解析推送通知在打开时崩溃

在打开推送通知时解析推送“未找到活动”

在 Mobilefirst V7.1 上使用 Clevertap 推送通知在启动画面上出现 iOS 应用程序崩溃问题

应用程序被杀死时点击推送通知后应用程序崩溃

使用关闭的应用推送通知。 [解析服务器]

在接收推送通知时,点击事件应用程序在导航后崩溃