title: Firebase message with high priority not waking device from Doze android 6+
body: <p>I have migrated my project from using GCM to use Firebase. Push notification comes through ok when the device is awake or been asleep recently but if I leave the device for say an hour, no push is sent until I wakeup the device.</p>
  
  <p>The Android docs say that if you need to wake a device to deliver a message, then use FireBase with priority set to high. It also says that device Admin apps are not subject to Doze restrictions, my app is a device admin app.</p>
  
  <p>I thought I would mention that when I migrated the project from GCM to FCM, I only specified the package name in the firebase console and not the fingerprint.</p>
  
  <p><strong><em>What I have tried.</em></strong></p>
  
  <ol>
  <li><p>Set priority to high</p>
  
  <pre><code>{
    "time_to_live": 300000,
    "delay_while_idle": false,
    "android": {
      "priority": "high"
    },
    "data": {
      "message": "PING_DEVICE",
      "time": "21/01/2018 16:20:28",
      "pushguid": "10062"
    },
    "registration_ids": [
      "eOMT........"
    ]
  }
  </code></pre>
  
  <p>Time to live is set so the message will come through eventually. delay_while_idle is set to false, this is ignored by FCM after sept 2016.</p></li>
  <li><p>Device admin apps are not subject to Doze, mine is a device admin app but I have also explicitly added the app to the Doze whitelist found in Setting -> Battery -> Optimization. This was done manually through the settings app and NOT programmatically in code.</p></li>
  </ol>
  
  <p>I have left my device to go to sleep for 3 hours and no push come through. I have also used adb to put the device into Doze. When adb puts the device in Doze no push is received, when adb takes the device out of Doze, the push comes through.</p>
  
  <p><strong><em>further thoughts I haven't tried.</em></strong></p>
  
  <p>My pushes are data messages. This is because I don't want the push to come to the notification bar on the device and have the user click it to execute the functionality. The user has no interaction with the device admin app. So a data message is handled by </p>
  
  <pre><code>onMessageReceived(RemoteMessage remoteMessage)
  </code></pre>
  
  <p>I believe notification messages do wake up the device, which is what I need but I want the app to handle the push, not the user. Could I have messages that are both notification and data but have onMessageRecievied handle the functionality?</p>
  
  <p>Has anybody experienced anything similar or have any solutions to this?</p>
  
  <p>[EDIT1]
  I have found the following link below that says you can send a message which is both notification and data, but if the app is in the background, the notification is displayed but the data is only executed when the user clicks the notification. This is not what I want as I would like the data to execute in onMessageRecived straight away.</p>
  
  <p><a href="https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages" rel="noreferrer">notification with data</a></p>
  
  <p>[EDIT2]
  I have added the following code and permission to the app. The app now asks the user to whitelist the app for Doze, so I clicked yes. I then via adb put the device in Doze and sent a push. Nothing came through until I took the device back out of doze mode. So, unfortunately, this does not work.</p>
  
  <pre><code>if(Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
              Intent intent = new Intent();
              String packageName = getPackageName();
              PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
              if (!pm.isIgnoringBatteryOptimizations(packageName)) {
                  intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                  intent.setData(Uri.parse("package:" + packageName));
                  startActivity(intent);
              }
          }
  
  &lt;uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" /&gt;
  </code></pre>
  
  <p>[EDIT3]</p>
  
  <p>I have done further testing to try to isolate the problem and take my web application code out of the equation. I put the device into Doze via adb and the used FireBase console to send the push instead. <strong><em>The push came through correctly</em></strong> This tells me there is a problem with my web application code that sends all the push info to the fcm endpoint. I will get the code tonight and post later.</p>
  
  <p>[EDIT4]
  i have just done some more testing. I placed the device into doze then used FireBase console to send a data message with 2 key-value pairs. When the device is in Doze and the app is in the foreground (on the screen), the push comes through and onMessageReceived executes. This is great. However, if the app is in the BG then only a notification is displayed. I understand that from the docs, the data messages are dispatched to the launcher activity via an Intent, but my launcher app does not handle the pushes.The class that handles the pushes is called MyAndroidFirebaseMsgService and extends FirebaseMessagingService.</p>
  
  <p>Do i have to route the intent to this class in case the app is in the BG? Seems a bit starnge to have to do this. It was never the case in GCM.</p>
  
  <p><strong>Also, i do not want the app launching from a push as this is very invasive as the device user could be using a different app. My app is also a device admin app, so 99% of the time there is no user interaction, it is just a client that executes policies on the device.</strong></p>
  
  <p>[edit5]</p>
  
  <pre><code>internal static void SendNotification (  Dictionary&lt;string, string&gt; nameValues ,  List&lt;string&gt; theregIDs , string sPushName)
           {     
              string stringregIds =  string.Join("\",\"", theregIDs) ;
  
               JavaScriptSerializer js = new JavaScriptSerializer();
              string keyValueJson = js.Serialize(nameValues);
  
              string TIME_TO_LIVE = "604800";
  
              string DELAY_WHILE_IDLE = "false";
  
              string ENDPOINTADDRESS = @"https://fcm.googleapis.com/fcm/send";
  
  
              postData = String.Concat("{\"time_to_live\":", TIME_TO_LIVE,  ",\"delay_while_idle\": ", DELAY_WHILE_IDLE,  ",  \"android\":{\"priority\":\"high\" } ,\"data\": { \"message\" : " + "\"" + sPushName + "\",\"time\": " + "\"" + System.DateTime.Now.ToString() + "\""
                  , keyValueJson
                 , "},\"registration_ids\":[\"" + stringregIds + "\"]}");
  
  
              WebRequest myWebRequest = null;
              WebResponse myWebResponse = null;
              try
              {
                  myWebRequest = WebRequest.Create(ENDPOINTADDRESS);                         
                  myWebRequest.Method = "post";
                  myWebRequest.ContentType = "application/json";
                  //  myWebRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
                  myWebRequest.Headers.Add("Authorization: key=" + Our_Api_Key);
                  myWebRequest.Headers.Add("Sender:id=" + Our_Sender_Id);
  
                  Byte[] BA = Encoding.UTF8.GetBytes(postData);
                  myWebRequest.ContentLength = BA.Length;
  
                  using (Stream dataStreamOut = myWebRequest.GetRequestStream())
                  {
                      dataStreamOut.Write(BA, 0, BA.Length);
  
                  }
  
                  using (myWebResponse = myWebRequest.GetResponse())
                  {
                      using (Stream dataStream = myWebResponse.GetResponseStream())
                      {
                          using (StreamReader tReader = new StreamReader(dataStream))
                          {
                              strServerResponse = tReader.ReadToEnd(); 
                          }
  
                      }
                  }
  
  
              }
              catch (WebException ex)
              {
  
  
  
              }
  
           }//
  </code></pre>
  
  <p>thanks</p>