安卓开发使用Notifiation推送通知没有声音、振动等

编程入门 行业动态 更新时间:2024-10-26 21:25:24

安卓开发使用Notifiation推送通知没有<a href=https://www.elefans.com/category/jswz/34/1770581.html style=声音、振动等"/>

安卓开发使用Notifiation推送通知没有声音、振动等

安卓开发使用Notifiation推送通知没有声音、振动等

    • 写在前面的话
    • 问题表现
    • 权限方面
    • 控制台相关警告
    • 其他说明
    • 2019/10/22 再次更新,手机版本

写在前面的话

这里不打算写教程,网上一搜大同小异,但是遇到的问题却让人摸不到头脑,我去安卓官方查看doc也没发现哪里写错。非常疑惑,所以记录下来,解决后作为自己经验的累积。

问题表现

  1. 通知可以发出,但
    builder.setAutoCancel(true) .setDefaults(NotificationCompat.DEFAULT_ALL)
    (点击自动消失,设置声音振动等为系统默认)无效果,然而
    builder.setContentTitle("title") .setSmallIcon(R.mipmap.ic_launcher_new_round) .setContentText(serverMessage) .setContentIntent(messagePendingIntent)
    是有效的。

  2. 我也分别使用过 Notification.Builder和NotificationCompat.Builder 声明builder以及使用
    notification.flags = Notification.XXXXXX 的方式设置声音振动呼吸灯等,依然没有效果;

权限方面

权限方面,这里只列出部分,相关的大概有以下:

<uses-permission android:name="android.permission.INTERNET" />
<!-- android 9.0上使用前台服务,需要添加权限 -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<!-- 允许程序振动 -->
<uses-permission android:name="android.permission.VIBRATE" />
<!-- 允许程序在手机屏幕关闭后后台进程仍然运行 -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- 允许程序禁用键盘锁 -->
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<!--闪光灯-->
<uses-permission android:name="android.permission.FLASHLIGHT"/>

控制台相关警告

调制控制台输出Warning,不知道是否与这个有关:

W/Notification:Use of stream types is deprecated for operations other than volume control
W/Notification:See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case

我也尝试过用setSound()设置uri来指定铃声,但依然无效,给出来的警告不变。

其他说明

  1. API版本:分别使用Android 8.0,Android 8.1,Android 9.0测试都无效
  2. 手机:小米 8(Android 9)

– 2019/4/22 8:42 –

– 2019/10/22 16:08 –
没想到会有一天再翻出这篇文章。
上面的问题由于项目弃置,所以就一直没解决。不过最近由于在新的项目里又用了Notification来推送通知。这次的app是从头到尾我一人完成。因此并没有出现之前的问题。

2019/10/22 再次更新,手机版本

  1. API 版本:Android 8.0
  2. 手机 小米8 (Android 9)

MainActivity.java

//启动状态栏通知Service
Intent intent = new Intent(this, NotificationService.class);
intent.setAction("app.SERVICE");
startService(intent);

NotificationService.java

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.os.IBinder;/*** @author hiYuzu* @version V1.0* @description 推送通知Service* @date 2019/10/15 10:24*/
public class NotificationService extends Service {private static final String TAG = NotificationService.class.getSimpleName();private static final String CHANNEL_ID = "NotificationService";//线程获取信息private MessageThread messageThread = null;@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {return START_STICKY;}@Overridepublic void onCreate() {super.onCreate();messageThread = new MessageThread();messageThread.isRunning = true;messageThread.start();}class MessageThread extends Thread {//循环控制boolean isRunning = true;public void run() {while (isRunning) {try {//间隔60sThread.sleep(60000);//推送消息if (MainActivity.notifyMessage) {notifyMessage();}} catch (Exception e) {e.printStackTrace();}}}}private void notifyMessage() {NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "报警通知", NotificationManager.IMPORTANCE_HIGH);channel.enableLights(true);//提示灯channel.setLightColor(Color.GREEN);//提示灯颜色channel.setShowBadge(true);//显示Logochannel.setDescription("通知的描述");channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);//设置锁屏可见if (manager != null) {manager.createNotificationChannel(channel);}Notification notification = new Notification.Builder(this, CHANNEL_ID).setContentTitle("标题").setContentText("内容").setSmallIcon(R.mipmap.ic_launcher_round).setAutoCancel(true)
//                .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0)).build();startForeground(1, notification);}@Overridepublic void onDestroy() {messageThread.isRunning = false;super.onDestroy();}
}

补充说明:

  1. 代码经过修改,实际的描述、标题、内容都可以通过后台获取。
  2. 除了使用 startService(Intent) 方法启动服务,还可以使用 startForegroundService(Intent) 启动。但是要注意的是,使用 startForegroundService(Intent) 时,服务要在 5s 内 startForeground,否则会出错导致应用闪退。在 stackOverflow 上看了一下,有人说这是 BUG,以后会修复。

更多推荐

安卓开发使用Notifiation推送通知没有声音、振动等

本文发布于:2024-02-25 18:18:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1699878.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:声音   通知   卓开发   Notifiation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!