我在Android应用程序中使用Parse Notification,我看到了Parse Notification的教程,其中显示了类型PushService的方法setDefaultPushCallback,但是当我在我的应用程序中使用此方法时,它不可用。
任何其他解决方案而不是此方法我该如何实现它?
MainActivity代码:
ParseAnalytics.trackAppOpenedInBackground(getIntent());
PushService.setDefault
Parse App:
public class ParseApp extends Application {
@Override
public void onCreate() {
super.onCreate();
Parse.initialize(this, "",
"");
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL,true);
}
} // Update - code formatting
答案 0 :(得分:1)
现在不推荐使用PushService.setDefaultPushCallback()。 因此,使用自定义解析广播接收器是灵魂,将以下内容添加到清单文件并更改名称
<receiver android:name="com.yourProject.YourReceiver" android:exported=false>
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.OPEN" />
<action android:name="com.parse.push.intent.DELETE" />
</intent-filter>
同样适用于班级:
public class CustomParseReceiver extends ParsePushBroadcastReceiver {
private Intent parseIntent;
public CustomParseReceiver() {
super();
}
@Override
protected void onPushReceive(Context context, Intent intent) {
super.onPushReceive(context, intent);
if (intent == null)
return;
try {
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
parseIntent = intent;
Intent broadcast = new Intent("Your Package Name");
LocalBroadcastManager.getInstance(context)
.sendBroadcast(broadcast.putExtra("message1",""));
} catch (JSONException e) {
Log.d("PushJsonException", "" + e.getMessage());
}
}
}
将此添加到您的MainActivity
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// When a Push Notification comes, this is called
// Call Methods to Update Your Stuff
}
};
@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, new IntentFilter("Your Package Name"));
}
@Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver);
}