如何覆盖 ParsePushBroadcastReceiver 的 onPushReceive()?
How to override onPushReceive() of ParsePushBroadcastReceiver?
我正在使用 Parse.com 的推送通知服务。根据 doc:
override onPushReceive to trigger a background operation for "silent"
pushes
我找到了 onPushOpen() 的源代码 here,但现在我必须重写 onPushReceive() 来自定义声音和振动的行为。我不知道我应该在onPushReceive()中做什么,有没有示例代码可以帮助我弄清楚onPushReceive()中的逻辑?谢谢
创建一个扩展 ParsePushBroadcastReceiver 的新 class:
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/
您可以使用 intent 额外参数 "action" 来调用您的 intent 来处理任何您想要的。
原 onPushReceive 来源:
protected void onPushReceive(Context context, Intent intent) {
JSONObject pushData = null;
try {
pushData = new JSONObject(intent.getStringExtra("com.parse.Data"));
} catch (JSONException var7) {
Parse.logE("com.parse.ParsePushReceiver", "Unexpected JSONException when receiving push data: ", var7);
}
String action = null;
if(pushData != null) {
action = pushData.optString("action", (String)null);
}
if(action != null) {
Bundle notification = intent.getExtras();
Intent broadcastIntent = new Intent();
broadcastIntent.putExtras(notification);
broadcastIntent.setAction(action);
broadcastIntent.setPackage(context.getPackageName());
context.sendBroadcast(broadcastIntent);
}
Notification notification1 = this.getNotification(context, intent);
if(notification1 != null) {
ParseNotificationManager.getInstance().showNotification(context, notification1);
}
}
如果意图中没有额外的 "alert" 或 "title",则不会通知。
因此您根本不需要扩展任何 class 来进行静默推送更新...
我正在使用 Parse.com 的推送通知服务。根据 doc:
override onPushReceive to trigger a background operation for "silent" pushes
我找到了 onPushOpen() 的源代码 here,但现在我必须重写 onPushReceive() 来自定义声音和振动的行为。我不知道我应该在onPushReceive()中做什么,有没有示例代码可以帮助我弄清楚onPushReceive()中的逻辑?谢谢
创建一个扩展 ParsePushBroadcastReceiver 的新 class:
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/
您可以使用 intent 额外参数 "action" 来调用您的 intent 来处理任何您想要的。
原 onPushReceive 来源:
protected void onPushReceive(Context context, Intent intent) {
JSONObject pushData = null;
try {
pushData = new JSONObject(intent.getStringExtra("com.parse.Data"));
} catch (JSONException var7) {
Parse.logE("com.parse.ParsePushReceiver", "Unexpected JSONException when receiving push data: ", var7);
}
String action = null;
if(pushData != null) {
action = pushData.optString("action", (String)null);
}
if(action != null) {
Bundle notification = intent.getExtras();
Intent broadcastIntent = new Intent();
broadcastIntent.putExtras(notification);
broadcastIntent.setAction(action);
broadcastIntent.setPackage(context.getPackageName());
context.sendBroadcast(broadcastIntent);
}
Notification notification1 = this.getNotification(context, intent);
if(notification1 != null) {
ParseNotificationManager.getInstance().showNotification(context, notification1);
}
}
如果意图中没有额外的 "alert" 或 "title",则不会通知。
因此您根本不需要扩展任何 class 来进行静默推送更新...