1. 程式人生 > >Android極光推送自定義通知問題

Android極光推送自定義通知問題

    private void showInspectorRecordNotification() {
        RemoteViews customView = new RemoteViews(context.getPackageName(), R.layout.view_custom);
        customView.setTextViewText(R.id.tvName_inspectPlan, planInfo.convertlineId2lineName(context, MyApplication.getInstance().getAppData().getUserId()));
        customView.setTextViewText(R.id.tvTime_inspectPlan, planInfo.getPlanYm());
        customView.setTextViewText(R.id.tvPlanSate_inspectPlan, planInfo.convertstateId2stateText(context));

        NotificationCompat.Builder mBuilder = new Builder(context);
        mBuilder.setContent(customView)
                .setContentIntent(getDefalutIntent(PendingIntent.FLAG_UPDATE_CURRENT))
                .setWhen(System.currentTimeMillis())
                .setTicker("")
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setOngoing(false)
                .setSmallIcon(R.mipmap.icon);
        Notification notify = mBuilder.build();
        notify.contentView = customView;
        notify.flags |= Notification.FLAG_AUTO_CANCEL; // 點選通知後通知欄消失
        // 通知id需要唯一,要不然會覆蓋前一條通知
        int notifyId = (int) System.currentTimeMillis();
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(notifyId, notify);
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        if (null == mNotificationManager) {
            mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }

        this.context = context;

        Bundle bundle = intent.getExtras();
        Util.soutLong(TAG, "onReceive - " + intent.getAction());

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {//註冊
            Util.soutLong(TAG, "JPush使用者註冊成功");
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Util.soutLong(TAG, "接受到推送下來的自定義訊息");
            // 在這裡顯示自定義通知
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Util.soutLong(TAG, "接受到推送下來的通知");
            // 這裡會顯示極光推送預設的通知,自定義能力有限
        }
    }


效果是可以實現,不過當傳送的通知大於一條的時候,第二條通知會把第一條通知覆蓋。即只會顯示一條通知,查了一些資料後終於找到原因:

int notifyId = (int) System.currentTimeMillis(); 傳送通知的 notifyId 需要唯一通知才不會被覆蓋,否則系統認為是在更新通知

補充:前面的方法解決了推送只顯示一條的問題,在推送多條通知的時候假如資料型別相同的又會出現一個問題:前面的通知內容會被後面推送的通知內容所覆蓋:最終發現問題出在 PendingIntent.getActivity(引數1, 引數2, 引數3, 引數4);方法。 該方法有四個引數,問題主要出在第二個引數,API文件裡雖然說是未被使用的引數(給出的例子也直接寫0的),實際上是通過該引數來區別不同的Intent的,如果通知的第二個引數每次都相同,就會覆蓋掉之前的Intent了。所以總是獲取到最後一個Intent。

getDefalutIntent程式碼如下:

    /**
     * 構造一個開啟通知的Intent
     *
     * @param flags
     * @return
     */
    private PendingIntent getDefalutIntent(int flags) {
        Intent transferIntent = new Intent(context, LoadDataEmptyActivity.class);
        transferIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        transferIntent.putExtra(PushReceiverConstant.KEY_FORM_TYPE, pushType);

        // 第二個引數不能寫死,可以寫一個隨機數或者是時間毫秒數 保證唯一
        PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)(Math.random() * 100), transferIntent, flags);
        return pendingIntent;
    }