我使用FCM,当我的应用程序最小化或手机锁定时,我听不到声音和颤音。当某件事来自服务器时(当服务器向我的手机发送消息时),我会创建一个通知。当我的应用程序是在上面我听到一个声音和vibratteJava android FCM,当应用程序最小化或屏幕锁定时我听不到声音和颤音
private void createNotification(String messageBody) {
Bitmap Largeicon = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ic_launcher);
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_SYNCHRONIZE_ID);
Intent intent = new Intent(getApplicationContext(), NotificationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//FLAG_UPDATE_CURRENT is important
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK/* | PowerManager.FULL_WAKE_LOCK*/ | PowerManager.ACQUIRE_CAUSES_WAKEUP), "SMOK Komunal");
wakeLock.acquire();
wakeLock.release();
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification notification = new Notification.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Wiadomosc z serwera")
.setStyle(new Notification.BigTextStyle().bigText(messageBody))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setLargeIcon(Largeicon)
.setContentIntent(pendingIntent)
.setVibrate(vibratePattern)
.setLights(Color.GREEN, 2000, 2000)
.build();
notificationManager.notify(0, notification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK /*| PowerManager.FULL_WAKE_LOCK |*/ | PowerManager.ACQUIRE_CAUSES_WAKEUP), "SMOK Komunal");
if (wakeLock.isHeld())
wakeLock.release();
}
}