Android通知(Notification)

在Android开发中我们经常需要用到通知,比如聊天类应用的消息通知,咨询类应用的消息推送通知等。

今天我们就来了解一下,等一个新的消息过来的时候要如何弹出通知,用户点击通知的时候如何跳转到消息界面。

首先在Anroid官网上有一篇开发文档指导:https://developer.android.google.cn/guide/topics/ui/notifiers/notifications

Notification的开发主要分作两步:

1. 创建一个Notification

Notificaiton是通过NotificationCompat.Builder 来创建,NotificationCompat.Builder可以设置通知栏显示的图标,显示的标题与内容。

 

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
mBuilder.setSmallIcon(R.drawable.notification_icon) //设置通知栏中显示的图标
mBuilder.setContentTitle("My notification")               //通知的标题
mBuilder.setContentText("Hello World!");                  //通知的内容

通过这个设置就已经构建好一个完整的的Notification了,但是当你点击这个Notification时,这个Notification不会有任何反应。如果你想让你的Notification,在被点击时跳转到具体的内容界面,那么需要通过NotificationCompat.BuilderPendingIntent设置。

PendingIntent的构建可以通过两种方式:

1)通过TaskStackBuilder
Intent resultIntent = new Intent(this, ResultActivity.class);              //创建一个Intent跳转到目标的Activity

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

stackBuilder.addParentStack(ResultActivity.class);          // Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);

通过这种方式去创建设置PendingIntent,点击消息会跳转到ResultActivty,但是点击返回键时会直接退到主屏幕,而不是退回app的主界面。

一般情况下,我们在点击消息读取消息后,点击返回键时时需要返回我们app的主界面的,这需要我们去设置主界面的Activity作为任务栈的parent:

//ChatActivity是点击消息后需要去跳转的界面,这个Activity的启动模式必须为singleTask,action为Intent.ACTION_MAIN,category为Intent.CATEGORY_LAUNCHER 。
Intent resultIntent = ChatActivity.getCallingIntent(this, userId, userName);
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
//app的主界面的Activity,用户在读取消息之后点击返回会返回到这个界面
stackBuilder.addParentStack(new ComponentName(this,
"cc.test.patient.view.activity.MainActivity"));
stackBuilder.addNextIntentWithParentStack(resultIntent);

2)通过PendingIntent.getActivities方式

Intent[] appIntent = makeIntentStack(context, userName, userId);
appIntent[1].setAction(Intent.ACTION_MAIN);
appIntent[1].addCategory(Intent.CATEGORY_LAUNCHER);
//关键的一步,设置启动模式,如果设置启动模式失效会直接跳转到parent activity,我是直接在manifest文件中进行了设置
appIntent[1].setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
PendingIntent contentIntent = PendingIntent.getActivities(context, 0, appIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
private Intent[] makeIntentStack(Context context, String userName, String userId) {

    Intent[] intents = new Intent[2];

    intents[0] = Intent.makeRestartActivityTask(new ComponentName(context,
            "cc.test.patient.view.activity.MainActivity"));

    intents[1] = ChatActivity.getCallingIntent(context, userName, userId);

    return intents;
}

2. NotificationManager发送通知

//获取NotificationManager服务

NotificationManager mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.

Notification notification = mBuilder.build();
mNotificationManager.notify(mId, notification);         //发送Notification

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值