android - 使用 ParsePushBroadcastReceiver 自定义 BigTextStyle 通知

标签 android push-notification parse-platform

我正在尝试覆盖 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 方法,标准将呈现并单击它确实打开预期的 Activity 。

我找不到任何扩展 ParsePushBroadcastReceiver 以获取自定义通知的教程或示例。我错过了什么吗?

最佳答案

您将内容 Intent 设置为:

Intent detailsIntent = new Intent("com.parse.push.intent.OPEN", Uri.parse(url));
PendingIntent piDetails = PendingIntent.getService(context, 0, detailsIntent, 0);

这是在您的通知被点击时调用的 Intent 。但是,ParsePushBroadcastReceiver 是一个BroadcastReceiver。如果您想重用 Parse 提供的基础设施,您需要使用 PendingIntent.getBroadcast() 而不是 PendingIntent.getService()

当然,您可以只使用 PendingIntent.getActivity() 并使用您在 onPushOpen() 中构建的 Intent 并完全绕过该间接寻址。

关于android - 使用 ParsePushBroadcastReceiver 自定义 BigTextStyle 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26267555/

相关文章:

ios - 首次启动应用程序时调用 didReceiveRemoteNotification

ios - parse.com的缓存策略

javascript - 多个查询解析 Javascript

android - 用于地理定位的后台服务在 Phonegap (android) 中不起作用

javascript - 动态地将数据放入选择选项下拉列表(jQuery)

swift - 使用 FCM 的推送通知不适用于我的所有设备

ios - 推送通知 iOS 重置

android - Android 菜单中的 ResXMLTree_node header 大小 0x0 太小

java - Android 手势不起作用

android - 使用 Parse.com 进行离线缓存?