简体   繁体   中英

customize BigTextStyle notification with ParsePushBroadcastReceiver

I'm trying to override the getNotification() method of Parse ParsePushBroadcastReceiver class to render a BigTextStyle notification with two action buttons at the bottom. Sort of like: http://developer.android.com/design/media/notifications_pattern_two_actions.png

According to the Parse push notification guide, that's all I need to do:

Here's my code:

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);
    }
}

The BigTextStyle notification does show up. But clicking on the notification or the action buttons doesn't do anything. And as the log shows, onPushOpen was never invoked:

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

If I don't use my extended ParsePushBroadcastReceiver class, or if I use my ParsePushBroadcastReceiver class but not overriding the getNotification method, the standard would render and clicking on it does open the intended activity.

I can't find any tutorial or example of extending ParsePushBroadcastReceiver to get custom notification. Am I missing anything?

You set your content intent to:

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

This is the intent that is called when your notification is clicked. However, the ParsePushBroadcastReceiver is a BroadcastReceiver . If you want to reuse the infrastructure provided by Parse, you need to use PendingIntent.getBroadcast() rather than PendingIntent.getService() .

Of course, you could just use PendingIntent.getActivity() and use the Intent you built in onPushOpen() and bypass that indirection completely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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