关闭应用程序后解析推送通知崩溃
Parse push notification crashs after the app is dismissed
是的,我知道 Parse 将于明年关闭,但出于某种原因,我现在必须使用它的功能。这是怎么回事,当应用程序处于活动状态时,推送通知部分工作正常,甚至在后台运行,但如果我关闭该应用程序,这意味着他不再 运行,当我的应用程序收到推送时它会崩溃通知。我搜索了很多,所有人都告诉我把 Parse.initialize(...) 放在应用程序上而不是 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;
}
好像是最近发布的parse lib版本引起的,所以我从compile 'com.parse:parse-android:1.+'
降级到编译'com.parse:parse-android:1.10.1'
,现在已经解决了。另外,我必须删除 installation.put("GCMSenderId", this.getString(R.string.google_app_id));
部分才能在应用程序死机时接收通知。
是的,我知道 Parse 将于明年关闭,但出于某种原因,我现在必须使用它的功能。这是怎么回事,当应用程序处于活动状态时,推送通知部分工作正常,甚至在后台运行,但如果我关闭该应用程序,这意味着他不再 运行,当我的应用程序收到推送时它会崩溃通知。我搜索了很多,所有人都告诉我把 Parse.initialize(...) 放在应用程序上而不是 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;
}
好像是最近发布的parse lib版本引起的,所以我从compile 'com.parse:parse-android:1.+'
降级到编译'com.parse:parse-android:1.10.1'
,现在已经解决了。另外,我必须删除 installation.put("GCMSenderId", this.getString(R.string.google_app_id));
部分才能在应用程序死机时接收通知。