android – 在推送通知上振动

android – 在推送通知上振动,第1张

概述首先,我检查了所有这些链接: > How to Call Vibrator inside a Service in android > Android notifications and vibrate from service > Android GCM turn on Lights 但是当我收到推送通知时,我无法实现手机振动.这是我的代码: PushReceiver public class 首先,我检查了所有这些链接:

> How to Call Vibrator inside a Service in android
> Android notifications and vibrate from service
> Android GCM turn on Lights

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

PushReceiver

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 });        notificationmanager notificationmanager =                (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);        notificationmanager.notify(ConstantUtils.NOTIFICATION_ID_ORDER_READY,notificationBuilder.build());    }}

允许

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

测试

Device: Nexus 5

AndroID version: 6.0.1

为了让它发挥作用,我应该做些什么样的未知巫术?

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

该值应为以下一个或多个字段与按位或(|):DEFAULT_SOUND,DEFAULT_VIBRATE,DEFAULT_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,sleep notificationBuilder.setVibrate(new long[] { 1000,1000});
总结

以上是内存溢出为你收集整理的android – 在推送通知上振动全部内容,希望文章能够帮你解决android – 在推送通知上振动所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1137577.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存