2016-06-07 137 views
1

问题通知振动,声音,当屏幕被锁定

我收到GCM通知,但是当我的屏幕被锁定或关闭,然后我的震动和声音不工作。 我试图添加wakelock,但通过GCMListenerService的外观,当触发通知时,已经有一个清醒的意图。 我该怎么做才能使它工作?我失踪的棋子在哪里?

编辑

  • 当屏幕打开和解锁我的震动工作。
  • 我发现GcmListenerService负责部分,用于接收GCM推送通知,它看起来像唤醒锁定是越来越删除(下面的代码)

Bundle var2 = var1.getExtras(); var2.remove("message_type"); var2.remove("android.support.content.wakelockid");

CODE

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_notification_icon) 
      .setContentTitle(getString(R.string.new_notification)) 
      .setContentText(body) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setVibrate(new long[] {1000, 1000, 1000}) 
      .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary)) 
      .setContentIntent(pendingIntent); 

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

    Notification notification = notificationBuilder.build(); 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    notification.defaults |= Notification.DEFAULT_SOUND; 

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG"); 
    wl.acquire(10000); 

    notificationManager.notify(0, notification); 
+0

你加'<使用许可权的android:NAME =“android.permission.VIBRATE” />' –

+0

@Logic是的,当然 – JakubW

回答

0

所以事实证明,当你的通知包含data payloadnotification payload那么Android的锁定模式从即使寿你已经代码自定义一个playload创建通知。不需要

唤醒锁定 - GcmListenerService将决定它自己如果需要,只要你有<uses-permission android:name="android.permission.WAKE_LOCK" />

0

您需要在您的服务器端代码上添加此标志delay_while_idle=true。 看到here

+0

根据你发布的链接,当数据没有被设备收到时,这个标志是必需的。在我的情况下,我确实收到通知及其所有数据。唯一的问题是它的声音和震动没有被触发。 – JakubW

+0

是的,我明白你的意思,当设备处于睡眠模式或解锁状态时,你没有收到通知。我在服务器端设置了delay_while_idle = false,现在按照我的预期工作。 – Meenaxi