欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android Notification使用方法总结

时间:2021-05-14 10:39:40|栏目:Android代码|点击:

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调用。这样可以达到进入程序后,通知自动取消的效果
}

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

上一篇:Android小程序实现访问联系人

栏    目:Android代码

下一篇:详解Android 在 ViewPager 中使用 Fragment 的懒加载

本文标题:Android Notification使用方法总结

本文地址:http://www.codeinn.net/misctech/121355.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有