【问题标题】:Notification Sound, Vibration and LED don't work通知声音、振动和 LED 不工作
【发布时间】:2017-04-09 11:17:31
【问题描述】:

我正在尝试为我的应用中的通知设置自定义声音、振动和 LED 颜色 - 但它不起作用。设置标题、图标、颜色等其他一切都可以正常工作。我已经尝试了 Stackoverflow 中建议的许多解决方案,但它们也没有奏效,所以我在问一个问题。

这是我的通知代码 -

    Intent resultIntent = new Intent(this, ActivityB.class);
    Bundle b = new Bundle();
    //Some bundle related Code
    resultIntent.putExtra("bundle",b);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntent(new Intent(this, ActivityA.class));
    stackBuilder.addNextIntent(resultIntent);

    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

    android.support.v7.app.NotificationCompat.Builder builder = new android.support.v7.app.NotificationCompat.Builder(this);
    builder.setSmallIcon(R.drawable.ic_small_logo);
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_logo_large));
    builder.setContentTitle(notification.getTitle());
    builder.setContentText(notification.getBody());
    builder.setColor(Color.parseColor("#FFFFFF"));
    builder.setStyle(new NotificationCompat.BigTextStyle());
    builder.setVibrate(new long[] { 1000, 100, 1000, 100, 1000 });
    builder.setLights(Color.YELLOW, 3000, 3000);
    builder.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notif1));
    builder.setAutoCancel(true);
    builder.setContentIntent(resultPendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(1, builder.build());

我也添加了振动权限。在运行 Lollipop 和 Marshmallow 的 2 部手机上对此进行了测试。

编辑 1:

共享我的应用程序使用的所有权限 -

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.VIBRATE"/>

编辑 2: 适用于棉花糖版手机。不适用于带有 Lollipop 的手机。

编辑 3: 也适用于 Nougat(一加 3T 手机)。

【问题讨论】:

  • setVibrate() 将不起作用,除非您拥有 VIBRATE 权限。如果设备没有用于通知的 LED,setLights() 可能无法工作。
  • 我已经添加了振动权限,我在问题的结尾提到了这一点。我已经在使用 LED 通知的设备上进行了测试。
  • 尝试使用builder.setPriority() developer.android.com/reference/android/app/…
  • @MoosaBaloch 我将优先级设置为 PRIORITY_HIGH 但它不起作用..
  • 那么 .setDefaults(Notification.DEFAULT_ALL) 是否按预期工作?尝试将所有这些设置设置为默认值,以检查您的通知是否正确构建..

标签: android push-notification notifications


【解决方案1】:

根据您的 cmets,您的手机本身似乎有问题。我在 (1) 中询问关于振动的问题,因为您正在设置 { 1000, 100, 1000, 100, 1000 },这是一个 1000 毫秒的延迟,然后是 100 毫秒的振动。这可能太少而无法检测到。

无论如何,我在某些设备上遇到了同样的振动问题,所以我直接使用了振动器服务。发出通知后我所做的就是这样。根据您在下面的评论,我还添加了铃声管理器部分。

// built the notification without vibration and sound
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
// start vibrator manually
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(new long[] {1000, 100, 1000, 100, 1000}, Constants.VIBRATION_ONCE);
// start sound manually
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notif1);
Ringtone ringtone = RingtoneManager.getRingtone(this, uri);
ringtone.play();

这似乎适用于大多数设备。至于通知来看,Official Document已经说明了

为设备上的 LED 指示灯设置所需的颜色,以及闪烁占空比(以毫秒为单位)。并非所有设备都会遵守所有(甚至任何)这些值。

如果您使用的是小米设备,请尝试“信任”该应用并启用通知所需的所有权限,包括“自动启动”。它适用于大多数情况。

【讨论】:

  • 今晚我会测试一下,然后告诉你。虽然振动解决方案有点小技巧 :D .. 你能告诉我如何解决声音问题吗?除了自己播放声音..
  • 从我在你的代码中看到的,这似乎是正确的。声音的格式是什么? .mp3?既然你朋友有声音,应该不是声音的问题,而是你的手机的问题。这两款手机非常少见。要么(1)你编写一个可以在你的设备上运行的代码,但是相当hacky,正如我为振动器所建议的那样(2)编写一个所谓的可用代码并希望大多数设备都能工作。
  • 是的,它是 mp3。我不能说问题出在手机上,因为我从其他应用程序(即 Whatsapp、Gmail、Facebook Lite 等)收到带有声音、灯光和振动的通知。但如果没有任何问题,那么我可能不得不遵循 (1)
  • 我还添加了使用RingtoneManager的代码
  • 今晚会测试并通知您
【解决方案2】:

声音不起作用的原因之一是它找不到来自builder.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notif1));的文件更改 到

String uriSound = String.format("android.resource://%s/raw/%s",getPackageName(), R.raw.notif1);

   builder.setSound(uriSound);

对于灯光,请使用 RGB 组合。 builder.setLights(Color.rgb(130, 130, 130));

将振动模式更改为更均匀的方式,

builder.setVibrate(new long[] { 50, 100, 150, 200, 250});

【讨论】:

  • 或灯光使用 hex.eg = builder.setLights.(0xCBD11A);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多