【问题标题】:Vibrate on push notification推送通知时振动
【发布时间】:2017-01-03 15:06:20
【问题描述】:

首先我检查了所有这些链接:

但是当我收到推送通知时,我无法实现手机振动。这是我的代码:

推送接收器

public class PushReceiver extends FirebaseMessagingService {
    public PushReceiver() {
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        if(remoteMessage.getData() != null){
            Map<String, String> data = remoteMessage.getData();
            sendNotification(data.get("message"));
        }
        else{
            if(remoteMessage.getNotification() != null) {
                sendNotification(remoteMessage.getNotification().getBody());
            }
        }
    }

    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, BaseActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_done_all_24dp)
                .setContentTitle(getString(R.string.str_notification_order_ready))
                .setContentText(messageBody)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        notificationBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


        notificationManager.notify(ConstantUtils.NOTIFICATION_ID_ORDER_READY, notificationBuilder.build());
    }
}

权限

<uses-permission android:name="android.permission.VIBRATE"/>

测试

设备:Nexus 5

安卓版本:6.0.1

我应该使用某种未知的巫术来使它起作用?

【问题讨论】:

  • 您可能需要检查此 SO solutionthis。这可能会帮助您解决问题。
  • @Mr.Rebot 很抱歉延迟回复,但这些解决方案都不适合我! :(

标签: android push-notification google-cloud-messaging android-service


【解决方案1】:

您还可以将setDefaults (int defaults) 用于您的NotificationCompat.Builder 实例,它为您提供默认的系统声音、振动和灯光通知。

该值应为以下字段中的一个或多个与位或(|)组合:DEFAULT_SOUNDDEFAULT_VIBRATEDEFAULT_LIGHTS

对于所有默认值,使用DEFAULT_ALL

例如根据您的代码,您正在设置默认声音,因此,如果您想设置默认声音和振动:

notificationBuilder.setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE);

如果你想要所有的默认设置,你可以通过设置notificationBuilder.setDefaults(-1)来实现它,它被认为是DEFAULT_ALL值。

setDefaults 见 android doc

编辑:

振动有 1000 毫秒的延迟。如果将第一个设置为 0,它将立即关闭。这是一种{延迟、振动、睡眠、振动、睡眠}模式

 // Each element then alternates between delay, vibrate, sleep, vibrate, sleep
 notificationBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000}); 

【讨论】:

  • setPriority() 适用于具有Oreo 但不适用于Pre-Oreo 的设备上的声音和振动。使用setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE) 终于可以正常工作了!
【解决方案2】:

这会使手机振动:

Vibrator v = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(500);

当你调用通知时也调用这个

【讨论】:

  • 伙计,我的服务中没有任何 mContext,但我可以在没有它的情况下调用 getSystemService,但是,它一直不振动:(
  • mContext是上下文,你可以用getActivity()代替
  • 对于服务,上下文就是服务本身。所以你应该为服务调用“this”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-17
  • 1970-01-01
  • 2020-06-26
  • 2017-09-13
  • 2021-11-04
相关资源
最近更新 更多