簡體   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