如何覆盖ParsePushBroadcastReceiver的onPushReceive()? [英] How to override onPushReceive() of ParsePushBroadcastReceiver?
问题描述
我使用Parse.com的推送通知服务。按照 DOC :
I am using push notification service of Parse.com. According to the doc:
覆盖onPushReceive,从而引发对后台操作沉默 推
override onPushReceive to trigger a background operation for "silent" pushes
我发现onPushOpen()这里的源$ C $ C < /一>,但现在我必须覆盖onPushReceive()自定义声音和振动的行为。我不知道我应该做的onPushReceive(),有什么样code,可以帮助我弄清楚里面onPushReceive逻辑()?谢谢你。
I found the source code of onPushOpen() here, but now I have to override onPushReceive() to customize the behavior of sound and vibration. I don't know what I should do in onPushReceive(), is there any sample code that help me figure out the logic inside onPushReceive()? Thanks.
推荐答案
创建扩展ParsePushBroadcastReceiver一个新的类:
Create a new class that extends ParsePushBroadcastReceiver:
public class MyPushBroadcastReceiver extends ParsePushBroadcastReceiver {
public static final String PARSE_DATA_KEY = "com.parse.Data";
@Override
protected Notification getNotification(Context context, Intent intent) {
// deactivate standard notification
return null;
}
@Override
protected void onPushOpen(Context context, Intent intent) {
// Implement
}
@Override
protected void onPushReceive(Context context, Intent intent) {
JSONObject data = getDataFromIntent(intent);
// Do something with the data. To create a notification do:
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentTitle("Title");
builder.setContentText("Text");
builder.setSmallIcon(R.drawable.ic_notification);
builder.setAutoCancel(true);
// OPTIONAL create soundUri and set sound:
builder.setSound(soundUri);
notificationManager.notify("MyTag", 0, builder.build());
}
private JSONObject getDataFromIntent(Intent intent) {
JSONObject data = null;
try {
data = new JSONObject(intent.getExtras().getString(PARSE_DATA_KEY));
} catch (JSONException e) {
// Json was not readable...
}
return data;
}
}
将此你的清单:
<receiver
android:name=".MyPushBroadcastReceiver"
android:exported="false">
<intent-filter>
<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>
更多信息:的http://www.androidhive.info/2015/06/android-push-notifications-using-parse-com/
这篇关于如何覆盖ParsePushBroadcastReceiver的onPushReceive()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!