繁体   English   中英

使用ParsePushBroadcastReceiver自定义BigTextStyle通知

[英]customize BigTextStyle notification with ParsePushBroadcastReceiver

我试图覆盖Parse ParsePushBroadcastReceiver类的getNotification()方法,以使用底部的两个操作按钮来呈现BigTextStyle通知。 有点像: http : //developer.android.com/design/media/notifications_pattern_two_actions.png

根据解析推送通知指南,这就是我要做的全部事情:

这是我的代码:

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以获得自定义通知的任何教程或示例。 我有什么想念的吗?

您将内容意图设置为:

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

这是单击您的通知时调用的意图。 但是, ParsePushBroadcastReceiverBroadcastReceiver 如果要重用Parse提供的基础结构,则需要使用PendingIntent.getBroadcast()而不是PendingIntent.getService()

当然,您可以只使用PendingIntent.getActivity()并使用内置在onPushOpen()的Intent并完全绕过该间接onPushOpen()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM