如何覆盖 ParsePushBroadcastReceiver 的 onPushReceive()?
Posted
技术标签:
【中文标题】如何覆盖 ParsePushBroadcastReceiver 的 onPushReceive()?【英文标题】:How to override onPushReceive() of ParsePushBroadcastReceiver? 【发布时间】:2015-03-10 09:14:07 【问题描述】:我正在使用 Parse.com 的推送通知服务。根据doc:
覆盖 onPushReceive 以触发“静默”的后台操作 推动
我找到了 onPushOpen() here 的源代码,但现在我必须重写 onPushReceive() 来自定义声音和振动的行为。我不知道我应该在 onPushReceive() 中做什么,是否有任何示例代码可以帮助我弄清楚 onPushReceive() 内部的逻辑?谢谢。
【问题讨论】:
【参考方案1】:创建一个扩展 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/
【讨论】:
我理解要覆盖的语法,但不知道 onPushReceive() 的逻辑。例如,我认为我应该调用 getNotification() 但还有什么?请帮帮我。 你可以在 onPushReceive 中为所欲为。例如,我用随机声音构建自己的通知并显示它。因此,如果它应该是静默推送,您可以更新数据库或在 onPushReceive 中执行其他操作 如何获得自定义通知以调用 onPushOpen? 欲了解更多信息,请参阅:androidhive.info/2015/06/…【参考方案2】:您可以使用意图额外参数“action”来调用您的意图来处理您想要的任何事情。
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);
如果意图中没有额外的“警报”或“标题”,则不会发出通知。
因此,您根本不需要扩展任何类来进行静默推送更新...
【讨论】:
以上是关于如何覆盖 ParsePushBroadcastReceiver 的 onPushReceive()?的主要内容,如果未能解决你的问题,请参考以下文章