我试图覆盖Parse ParsePushBroadcastReceiver类的getNotification()方法,以在底部呈现带有两个操作按钮的BigTextStyle通知。有点像: http://developer.android.com/design/media/notifications_pattern_two_actions.png
根据Parse推送通知指南,我需要做的就是:
这是我的代码:
public class NotificationReceiver extends ParsePushBroadcastReceiver {
private static final String LOG_TAG = NotificationReceiver.class.getSimpleName();
@Override
protected Notification getNotification(Context context, Intent intent) {
Log.v(LOG_TAG, "getNotification called");
Bundle extras = intent.getExtras();
String jsonData = extras.getString("com.parse.Data");
String url = "";
String objectId = null;
String title = "";
String alertMsg = "";
if (jsonData != null){
try {
JSONObject data = new JSONObject(jsonData);
objectId = data.getString("objectId");
}
catch(JSONException e){
Log.e(LOG_TAG, "Error parsing json data", e);
}
}
else{
Log.w(LOG_TAG, "cannot find notification data");
}
Log.v(LOG_TAG, "notification for item with id=" + objectId);
BucketListItem item = BucketListDao.getInstance().findById(objectId);
title = "Get ready!";
alertMsg = item.getSummary() + "\n" + item.getLocation() + "\n" + item.getStartTime();
Intent detailsIntent = new Intent("com.parse.push.intent.OPEN", Uri.parse(url));
PendingIntent piDetails = PendingIntent.getService(context, 0, detailsIntent, 0);
Intent snoozeIntent = new Intent(context, BucketListActivity.class);
snoozeIntent.setAction("com.stubhublabs.dopamine.SNOOZE");
PendingIntent piSnooze = PendingIntent.getService(context, 0, snoozeIntent, 0);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(alertMsg)
.setContentIntent(piDetails)
.setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(alertMsg))
.addAction (R.drawable.ic_stat_snooze,
context.getString(R.string.notification_snooze), piSnooze)
.addAction (R.drawable.ic_stat_details,
context.getString(R.string.notification_details), piDetails);
return builder.build();
}
@Override
protected void onPushOpen(Context context, Intent intent) {
Log.v(LOG_TAG, "onPushOpen called");
Intent i = new Intent(context, BucketListActivity.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
@Override
protected Class<? extends Activity> getActivity(Context context, Intent intent) {
Log.v(LOG_TAG, "getActivity called");
return super.getActivity(context, intent);
}
@Override
protected void onPushReceive(Context context, Intent intent) {
Log.v(LOG_TAG, "onPushReceive called");
super.onPushReceive(context, intent);
}
@Override
public void onReceive(Context context, Intent intent) {
Log.v(LOG_TAG, "onReceive Called");
super.onReceive(context, intent);
}
}
BigTextStyle通知确实显示出来。但是点击通知或操作按钮不会做任何事情。正如日志所示,从未调用onPushOpen:
10-08 15:03:28.776 4021-4021/ V/NotificationReceiver﹕ onReceive Called
10-08 15:03:28.776 4021-4021/ V/NotificationReceiver﹕ onPushReceive called
10-08 15:03:28.776 4021-4021/ V/NotificationReceiver﹕ getNotification called
10-08 15:03:28.776 4021-4021/ V/NotificationReceiver﹕ notification for item with id=yyw00Lh5Ce
如果我不使用我的扩展ParsePushBroadcastReceiver类,或者如果我使用我的ParsePushBroadcastReceiver类但不覆盖getNotification方法,则标准将呈现并点击它会打开预期的活动。
我无法找到任何扩展ParsePushBroadcastReceiver以获取自定义通知的教程或示例。我错过了什么吗?
答案 0 :(得分:1)
您将内容意图设置为:
Intent detailsIntent = new Intent("com.parse.push.intent.OPEN", Uri.parse(url));
PendingIntent piDetails = PendingIntent.getService(context, 0, detailsIntent, 0);
这是单击通知时调用的意图。但是,ParsePushBroadcastReceiver
是BroadcastReceiver
。如果您想重用Parse提供的基础架构,则需要使用PendingIntent.getBroadcast()
而不是PendingIntent.getService()
。
当然,您可以使用PendingIntent.getActivity()
并使用您在onPushOpen()
中构建的Intent并完全绕过该间接。