Android:屏幕锁定时通知不会振动/发出声音/闪烁

时间:2016-09-03 09:05:44

标签: android notifications push builder

我使用Firebase Messaging Service接收推送通知。 如果我的屏幕解锁,一切都运转良好。

当我的屏幕被锁定时,我收到通知,但我的手机没有发出任何声音,没有振动,我的灯也没有闪烁。

这是我生成消息的代码:

    private void sendNotification(String messageBody, String messageTitle) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setLights(Color.CYAN, 1, 1)
            .setPriority(Notification.PRIORITY_MAX)
            .setVisibility(Notification.VISIBILITY_PUBLIC)
            .setContentIntent(pendingIntent);

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

    notificationManager.notify(0, notificationBuilder.build());
}

2 个答案:

答案 0 :(得分:0)

使用此功能发送通知此工作正常

 private static final long[] vPattern = new long[]{1000, 1000, 1000, 1000, 1000};

 public static void sendNotification(Context ctx, NotificationData notificationData) {
        try {
            NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
            Integer notifiId = (int) System.currentTimeMillis();
            Intent intent_for_Notification = new Intent();
            Bundle b = new Bundle();
            b.putSerializable(Constants.NOTIFICATION_DATA, notificationData);
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx);
            mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationData.msg));
            mBuilder.setContentText(notificationData.msg);
            mBuilder.setContentTitle(ctx.getResources().getString(R.string.app_name));
            intent_for_Notification.putExtras(b);
            intent_for_Notification.setClass(ctx, SplashActivity.class);
            intent_for_Notification.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent contentIntent = PendingIntent.getActivity(ctx, notifiId, intent_for_Notification, 0);
            mBuilder.setContentIntent(contentIntent);
            mBuilder.setSmallIcon(R.mipmap.ic_launcher_196);
            mBuilder.setVibrate(vPattern);
            mBuilder.setPriority(Notification.PRIORITY_HIGH);
            mBuilder.setAutoCancel(true);
            mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
            mNotificationManager.notify(notifiId, mBuilder.build());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

答案 1 :(得分:0)

您是否在清单中加入了<uses-permission android:name="android.permission.WAKE_LOCK" />

  

使用唤醒锁定的一个合法案例可能是后台服务需要获取唤醒锁定以保持CPU在屏幕关闭时运行。但是,再次,这种做法应该最小化,因为它会影响电池寿命。 - Android Developers documentation

您可以尝试添加notificationBuilder.setDefault(defaults),因为我注意到即使您设置了.setSound(defaultSoundUri),某些手机也没有发出任何声音。

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle(messageTitle)
        .setContentText(messageBody)
        .setAutoCancel(true)
        .setSound(defaultSoundUri)
        .setLights(Color.CYAN, 1, 1)
        .setPriority(Notification.PRIORITY_MAX)
        .setVisibility(Notification.VISIBILITY_PUBLIC)
        .setContentIntent(pendingIntent);

//Added defaults
int defaults = 0;
    defaults |= android.app.Notification.DEFAULT_SOUND;
    defaults |= android.app.Notification.DEFAULT_VIBRATE;
    notificationBuilder.setDefaults(defaults);

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

notificationManager.notify(0, notificationBuilder.build());