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 需要唯一通知才不会被覆盖,否则系统认为是在更新通知


你可能感兴趣的:(Android极光推送自定义通知)