Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:当屏幕锁定时,通知不会振动/发声/闪烁_Android_Notifications_Push_Builder - Fatal编程技术网

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

Android:当屏幕锁定时,通知不会振动/发声/闪烁,android,notifications,push,builder,Android,Notifications,Push,Builder,我使用Firebase消息服务接收推送通知。 如果我的屏幕解锁,一切都很好 当我的屏幕被锁定时,我收到通知,但我的手机没有发出任何声音,没有振动,灯也没有闪烁 这是我生成消息的代码: private void sendNotification(String messageBody, String messageTitle) { Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Int

我使用Firebase消息服务接收推送通知。 如果我的屏幕解锁,一切都很好

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

这是我生成消息的代码:

    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());
}

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

 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();
        }
    }
您是否已将
包含在您的清单中

使用唤醒锁的一个合法案例可能是后台服务,它需要获取一个唤醒锁,以便在屏幕关闭时保持CPU运行以完成工作。不过,由于其对电池寿命的影响,这种做法也应尽量减少。-

您可以尝试添加
notificationBuilder.setDefault(默认值)
,因为我注意到,即使您设置了
.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());