2016-09-03 88 views
1

我使用Firebase消息傳遞服務來接收推送通知。 如果我的屏幕解鎖,一切都很好。Android:屏幕鎖定時,通知不會振動/發出聲音/閃爍燈光

當我的屏幕被鎖定時,我收到通知,但我的手機沒有發出任何聲音,沒有振動,燈光也沒有閃爍。

這是我的代碼來生成消息:

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