码迷,mamicode.com
首页 > 移动开发 > 详细

Android极光推送自定义通知

时间:2016-05-07 08:43:06      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:

    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极光推送自定义通知

标签:

原文地址:http://blog.csdn.net/yaoliangjun306/article/details/51330147

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!