通知栏的设置,点击跳转的设置

4. NotificationManager的设置

//这些都是直接设置通用的,没什么需要特殊说明的。
final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContent(mRemoteViews)
        .setTicker(info.messagetitle) //通知首次出现在通知栏,带上升动画效果的
        .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级
        .setAutoCancel(true)
        .setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
        .setDefaults(Notification.DEFAULT_VIBRATE);//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    //8.0以上弹出通知状态栏
    String channelID = BuildConfig.CHANNEL;
    String channelName = "XiaoNM";
    NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
    if (mNotificationManager != null) {
        mNotificationManager.createNotificationChannel(channel);
        mBuilder.setChannelId(channelID);
    }
}
Notification notify = mBuilder.build(); 
notify.icon = R.mipmap.ic_launcher; 
if (Build.VERSION.SDK_INT <= 10) { 
   notify.contentView = mRemoteViews; 
}
//不同类型的notify(由itemtype决定)在通知栏只保留一条记录,下面可以根据不同的type,显示成多条推送内容。
if (info.itemtype.equals("H5")) { 
    mNotificationManager.notify(NOTIFY_TAG, Utils.parseInt(info.linkurl), notify);
} else if (info.itemtype.equals("Channel")) {
    mNotificationManager.notify(NOTIFY_TAG, Utils.parseInt(info.itemid), notify);
} else {
    mNotificationManager.notify(NOTIFY_TAG, Utils.parseInt(info.itemtype), notify);
}

这里就说明一个属性setOngoing // 当时在做通知栏高度设置的时候,稍微体验了一下这个功能,设置了这个属性了,直接可以显示完整的UI,通知栏即使显示不下,这个也不会被折叠。一般用于播放器类。如QQ音乐,喜马拉雅听书等,在我们退到后台的时候,我们的通知栏上会有一个可以操作的简单界面。注意:设置了以后会有一个问题,不能被滑动移除该消息了。其他没什么,基本都有写注释。到了这里,通知栏就能正常显示了。

剩下最后一块就是,通知栏点击回到应用或者跳转到我们需要的界面。点击跳转通过PendingIntent实现。

5. PendingIntent的设置

PendingIntent pendingIntent = getContentIntent(context, info);

if (pendingIntent != null) {
    mBuilder.setContentIntent(pendingIntent);
}
//getContentIntent 是我自己写的对应的设置跳转界面的方法。根据自己的需求来写自己的逻辑。
PendingIntent pendingIntent = null; 
if (info.itemtype != null) {
    if (TextUtils.isEmpty(info.itemtype)) { 
        //空的无法识别的, 这边随便拿一个
        pendingIntent = getMainPendingIntent(context, info);
    }
}
private PendingIntent getMainPendingIntent(Context context, MessageInfo info) {
    Uri uri = Utils.makeMainScheme();// 这样的通讯一般通过scheme协议来进行跳转(scheme不会用的自行百度)
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    return PendingIntent.getActivity(context, 0, intent, 0);
}

到了这里整个推送到展示,到点击跳转就全部结束了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值