安卓开发使用Notifiation推送通知没有声音、振动等 您所在的位置:网站首页 为什么安卓泡泡没提示音 安卓开发使用Notifiation推送通知没有声音、振动等

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

2024-06-18 19:49| 来源: 网络整理| 查看: 265

安卓开发使用Notifiation推送通知没有声音、振动等 写在前面的话问题表现权限方面控制台相关警告其他说明2019/10/22 再次更新,手机版本

写在前面的话

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

问题表现

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

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

权限方面

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

控制台相关警告

调制控制台输出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来指定铃声,但依然无效,给出来的警告不变。

其他说明 API版本:分别使用Android 8.0,Android 8.1,Android 9.0测试都无效手机:小米 8(Android 9)

– 2019/4/22 8:42 –

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

2019/10/22 再次更新,手机版本 API 版本:Android 8.0手机 小米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; @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; } @Override public 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 { //间隔60s Thread.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);//显示Logo channel.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); } @Override public void onDestroy() { messageThread.isRunning = false; super.onDestroy(); } }

补充说明:

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


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有