android发送通知及更新通知

本文介绍了如何在Android中创建和更新通知,包括设置通知的图标、文本、声音、振动和LED灯光效果,以及如何使用NotificationManager和服务发送和管理通知。通过PendingIntent实现点击通知后的跳转,并探讨了通知的各种属性和标志,如FLAG_AUTO_CANCEL和FLAG_ONGOING_EVENT。
摘要由CSDN通过智能技术生成
/**
     * 发送通知
     */
    public void setNotification(){
        /**  start */
        //1.得到NotificationManager:
        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // 创建一个新的Notification对象,并添加图标
        notification = new Notification();
        // 通知显示的图标
        notification.icon = R.drawable.icon_bao;
        // 在状态栏(Status Bar)显示的通知文本提示,如:
        notification.tickerText = "收到一个新的通知";
        //发出提示音,如:
        notification.defaults |= Notification.DEFAULT_SOUND;//或
//        notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");//或
//        notification.sound = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "6");
        //填充Notification的各个属性:
        Context context = getApplicationContext();
        CharSequence contentTitle = "通知标题";
        CharSequence contentText = "通知内容";
        //点击通知跳转到哪里
        Intent notificationIntent = new Intent(this, CreateMemberActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        //在通知栏上点击此通知后自动清除此通知
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        //LED灯闪烁
        notification.defaults |= Notification.DEFAULT_LIGHTS;
//        或者可以自己的LED提醒模式:
//        notification.ledARGB = 0xff00ff00;
//        notification.ledOnMS = 300; //亮的时间
//        notification.ledOffMS = 1000; //灭的时间
//        notification.flags |= Notification.FLAG_SHOW_LIGHTS;
        //手机振动
        notification.defaults |= Notification.DEFAULT_VIBRATE;
//        或
//        long[] vibrate = {0,100,200,300};
//        notification.vibrate = vibrate;
        //发送通知
        mNotificationManager.notify(1, notification);
        /**  end */
    }

/**
     * 更新通知
     */
    public void updateNotification(){
        notification.tickerText = "收到第二个新的通知";
        Context context = getApplicationContext();
        CharSequence contentTitle = "点击跳转";
        CharSequence contentText = "跳转到添加会员";
        //点击通知跳转到哪里
        Intent notificationIntent = new Intent(this, MemberDetailsActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        mNotificationManager.notify(1, notification);
    }

通知是应用程序通知用户的一种方式,它无须活动,由通知管理器进行统一管理。通知包含一下功能:
1.      创建新的状态栏图标
2.      在扩展的状态栏窗口显示额外的信息(可以发起一个意图)
3.      闪烁/LED
4.      让手机震动
5.      发出声音(铃声,媒体库歌曲)
通知管理器是用来处理通知的系统服务,使用getSystemService方法可以获得对它的引用,如下:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);

通过使用通知管理器,可以触发新的通知,修改现有的通知或者删除那些不再需要的通知。首先创建一个新的Notification对象并传递给它要在状态栏显示的图标、状态栏的点击文本以及这个通知的时间。可以设置Notification对象的number属性来显示一个状态栏图标所表示的事件的数量。
Notification notification = new Notification(R.drawable.icon,"在EoeAndroidReceiver1中", System.currentTimeMillis());
可以通过两张方式在扩展的状态窗口配置通知的外观。
1.      使用setLatestEventInfo方法更新标准的扩展的状态通知显示中所显示的详细信息。
2.      使用一个远程视图(Remote View)设置contentView和contentIntent,以便为扩展的状态显示分配一个定制的UI。
最简单的方法是使用setLatestEventInfo方法来填充默认的状态窗口布局。标准的扩展的状态窗口布局会显示构造函数中定义的图标和时间,以及标题和一个详细信息字符串。

notification.setLatestEventInfo(context, "在EoeAndroidReceiver1中", null,contentIntent);

通知常用于请求用户的动作或注意,所以可以指定一个PendingIntent,当用户单击通知项的时候触发它,在大多数情况下,该意图应该打开应用程序,并导航到为通知提供了上下文的活动。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值