欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Notification使用方法总结

程序员文章站 2023-12-19 10:23:22
android notification使用方法总结 一. 基本使用 1.构造notification notificationcompat.builder...

android notification使用方法总结

一. 基本使用

1.构造notification

 notificationcompat.builder mbuilder = new notificationcompat.builder(appcontext)
          .setsmallicon(appcontext.getapplicationinfo().icon)
          .setwhen(system.currenttimemillis())
          .setautocancel(true)//当点击通知的时候会自动取消
          .setcontenttitle(contenttitle)
          .setticker(notifytext)//状态栏提示
          .setcontenttext(summarybody)
          .setcontentintent(pendingintent)
          .setnumber(notificationnum);
      notification notification = mbuilder.build();

2.显示通知

notificationmanager = (notificationmanager) context.getsystemservice(context.notification_service);
notificationmanager.notify(notifyid, notification);

3.手机震动提醒

/**
   * 手机震动和声音提示
   */
  public void viberateandplaytone(emmessage message) {
    if(message != null){
      if(emchatmanager.getinstance().isslientmessage(message)){
        return;
      } 
    }


    if (system.currenttimemillis() - lastnotifiytime < 1000) {
      // received new messages within 2 seconds, skip play ringtone
      return;
    }

    try {
      lastnotifiytime = system.currenttimemillis();

      // 判断是否处于静音模式
      if (audiomanager.getringermode() == audiomanager.ringer_mode_silent) {
        emlog.e(tag, "in slient mode now");
        return;
      }
      easesettingsprovider settingsprovider = easeui.getinstance().getsettingsprovider();
      if(settingsprovider.ismsgvibrateallowed(message)){//检测是否允许震动
        long[] pattern = new long[] { 0, 180, 80, 120 };
        vibrator.vibrate(pattern, -1);
      }

      if(settingsprovider.ismsgsoundallowed(message)){//检测是否允许声音
        if (ringtone == null) {
          uri notificationuri = ringtonemanager.getdefaulturi(ringtonemanager.type_notification);//获取系统默认通知铃声

          ringtone = ringtonemanager.getringtone(appcontext, notificationuri);
          if (ringtone == null) {
            emlog.d(tag, "cant find ringtone at:" + notificationuri.getpath());
            return;
          }
        }

        if (!ringtone.isplaying()) {//防止响铃叠加
          string vendor = build.manufacturer;

          ringtone.play();
          // for samsung s3, we meet a bug that the phone will
          // continue ringtone without stop
          // so add below special handler to stop it after 3s if
          // needed
          if (vendor != null && vendor.tolowercase().contains("samsung")) {
            thread ctlthread = new thread() {
              public void run() {
                try {
                  thread.sleep(3000);
                  if (ringtone.isplaying()) {
                    ringtone.stop();
                  }
                } catch (exception e) {
                }
              }
            };
            ctlthread.run();
          }
        }
      }
    } catch (exception e) {
      e.printstacktrace();
    }
  }

4.取消notification

void cancelnotificaton() {
    if (notificationmanager != null)
    notificationmanager.cancel(notifyid);//根据id取消,每个notification都有唯一的id。一般在activity的基类的onresume调用。这样可以达到进入程序后,通知自动取消的效果
}

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:

下一篇: