【问题标题】:Android notifications not making sound or vibrationAndroid 通知不发出声音或振动
【发布时间】:2012-01-26 22:32:46
【问题描述】:
int icon = R.drawable.icon;  
Context context = getApplicationContext();    
CharSequence contentTitle = "My notification";  
CharSequence contentText = "Countdown Complete!";     
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent();
Notification notification = new Notification(icon, "is completed!", System.currentTimeMillis());  
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND; 
notificationManager.notify(myCountDown.getId(), notification);

此代码在我的 android 应用程序中,我收到通知但没有声音或振动。

我已经在多部手机上进行了测试,所有手机都在设置中打开并打开了声音和振动。我还确保我要求在 android 清单中使用振动权限,但我仍然只收到通知...

我也试过了:

notification.defaults = Notification.DEFAULT_ALL;

notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND;

如何在通知中获得声音和振动?

【问题讨论】:

    标签: java android notifications


    【解决方案1】:

    不要忘记启用铃声和通知的振动设置。转到设置-> 声音。勾选“发出振动声”。

    【讨论】:

    • 这是用户常见的错误......它发生在我身上
    【解决方案2】:

    notification.defaults |= Notification.DEFAULT_VIBRATE; 不行吗?

    【讨论】:

    • 抱歉忘了说,但我试过 Notification.DEFAULTS_ALL 和 VIBRATE 还是什么都没有......
    • 您确实尝试像在文档中那样创建您的意图?Intent notificationIntent = new Intent(this, MyClass.class);
    【解决方案3】:

    第 1 步:确保您在清单中拥有 VIBRATE 权限,并且您在具有振动电机的设备上运行。

    第 2 步:删除 getApplicationContext(),因为这里不需要它。

    第 3 步:尝试通过 Notification 上的 sound 数据成员指定实际铃声,看看是否是默认不播放的问题(您需要在 flags 中没有 DEFAULT_SOUND) .

    【讨论】:

    • 第 1 步:伙计,我在我的问题中说过“我还确保我要求在 android 清单中使用振动权限”......我确实说过电话...... .
    • 第 2 步:摆脱它?将其设置为空?或删除参数?...两者都不起作用... null 给我运行时异常,并且没有 arg 无法编译...
    • 第 3 步:我正在使用 DEFAULT_SOUND,因为我想让它播放默认声音......但我仍然应该得到振动......
    【解决方案4】:
    Notification notif ... //create your notification
    

    notif.defaults |= Notification.DEFAULT_SOUND; notif.defaults |= Notification.DEFAULT_VIBRATE;

    不要忘记在清单中包含振动权限。

    【讨论】:

      猜你喜欢
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多