【发布时间】:2017-01-03 15:06:20
【问题描述】:
首先我检查了所有这些链接:
- How to Call Vibrator inside a Service in android
- Android notifications and vibrate from service
- Android GCM turn on Lights
但是当我收到推送通知时,我无法实现手机振动。这是我的代码:
推送接收器
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
我应该使用某种未知的巫术来使它起作用?
【问题讨论】:
标签: android push-notification google-cloud-messaging android-service