Java android FCM,当应用程序最小化或屏幕锁定时,我听不到声音和震动

时间:2017-04-03 05:57:44

标签: java 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();
        }
    }

1 个答案:

答案 0 :(得分:0)

试试这个

.setDefaults(Notification.DEFAULT_SOUND)