title: Push notification firebase sometime not received
body: <p>I have problem that sometimes not received notification. In order to receive notification, I need to restart my application then execute <code>notification.php</code> and notification will appear.But after 30 minute/15 minute when i try again running <code>notification.php</code> , not received any notification (I need to run my app again). I hope you guys can help me solve this problem.Thank you in advance.</p>
  
  <p><strong>notification.php</strong></p>
  
  <pre><code>&lt;?php
  
  require "../../init.php";
  include("../../function_lib.php");
  
  $id  = $_GET['id'];
  
  $title = "Try App";
  $message = "news..";
  $path_to_fcm = "https://fcm.googleapis.com/fcm/send"; //send push notification through this firebase url
  $server_key = "####";
  
  $topic = "/topics/reminder";
  $type = "reminder";
  
  
  
  //create http request to firease server
  //request need 2 section : 1.header section 2. payload section
  //add push notification in payload section
  
  //header section for http request : that content authorization key and content_type of this application
  
  //below are the header section of the http request
  $headers = array(
              'Authorization:key='.$server_key,
              'Content-Type:application/json'
          );
  
  
  
  /*
  
  $field = array('to'=&gt;$topic,'notification'=&gt;array("title"=&gt;"add title","text"=&gt;"add text","click_action"=&gt;"OPEN_ACTIVITY_1"),
                  'data'=&gt;array('title'=&gt;$title,'body'=&gt;$message,'type'=&gt;$type,'id'=&gt;$id),'priority'=&gt;"high");
  */
  //echo "\n".$key;
  // to = refer to fcm token, notification refer to message that wull be use in push notification.
  ///below are the payload section.
  $field = array('to'=&gt;$topic,
                  'data'=&gt;array('title'=&gt;$title,'body'=&gt;$message,'type'=&gt;$type,'id'=&gt;$id));
  
  //perform json encode
  $payload = json_encode($field);
  
  
  //pass $payload (content json encode) variable to firebase server
  
  //below  gonna start url section.gonna use firebase url http to send json to firebase server.
  $curl_session = curl_init();
  curl_setopt($curl_session,CURLOPT_URL, $path_to_fcm);
  curl_setopt($curl_session,CURLOPT_POST,true);
  curl_setopt($curl_session,CURLOPT_HTTPHEADER,$headers);
  curl_setopt($curl_session,CURLOPT_RETURNTRANSFER,true); 
  curl_setopt($curl_session,CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($curl_session,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4);
  curl_setopt($curl_session,CURLOPT_POSTFIELDS, $payload);
  
  //execute curl
  $result = curl_exec($curl_session);
  
  //close curl
  curl_close($curl_session);
  
  //close  mysqli
  //mysql_close($conn);
  
  
  $path = "../dashboard.php?act=home";
  echo ('&lt;script&gt;location.href="'.$path.'"&lt;/script&gt;');
  
  ?&gt;
  </code></pre>
  
  <p><strong>AndroidManifest.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mas.khoi.infoevent"&gt;
  
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/icon_logo_only"
          android:label="@string/app_name"
          android:roundIcon="@mipmap/ic_launcher_round"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"&gt;
  
  
          &lt;service android:name=".firebase.FcmInstanceIdService"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="com.google.firebase.INSTANCE_ID_EVENT" /&gt;
              &lt;/intent-filter&gt;
          &lt;/service&gt;
          &lt;service android:name=".firebase.FcmMessagingService"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="com.google.firebase.MESSAGING_EVENT" /&gt;
              &lt;/intent-filter&gt;
          &lt;/service&gt;
  
          &lt;activity android:name=".ScreenActivity"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity android:name=".fragment.MainActivity"&gt;
  
          &lt;/activity&gt;
          &lt;activity android:name=".EventDetailsActivity"&gt;
          &lt;/activity&gt;
  
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p><strong>FcmInstanceIdService.java</strong></p>
  
  <pre><code>public class FcmInstanceIdService extends FirebaseInstanceIdService {
  
      @Override
      public void onTokenRefresh() {
  
          //http://stackoverflow.com/questions/38340526/firebase-fcm-token-when-to-send-to-server
          String recent_token = FirebaseInstanceId.getInstance().getToken();
  
          //add token to sharepreference
          SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(Config.SETTING, Context.MODE_PRIVATE);
          SharedPreferences.Editor editor = sharedPreferences.edit();
          Log.i("generate token",recent_token);
          editor.putString(Config.SETTING_FCM_TOKEN,recent_token);
          editor.commit();
      }
  }
  </code></pre>
  
  <p><strong>FcmMessagingService.java</strong></p>
  
  <pre><code>public class FcmMessagingService extends FirebaseMessagingService {
  
  
      @Override
      public void onMessageReceived(RemoteMessage remoteMessage) {
          //if want to use function below need to used change data to notification at $field at server side.
          //String message = remoteMessage.getNotification().getBody();
          //String title = remoteMessage.getNotification().getTitle();
  
          if(remoteMessage.getData().size()&gt;0){
              Log.i("Notification Sucess",""+remoteMessage.getData());
              startNotification(remoteMessage);
          }else{
              Log.i("Notification Failed",""+remoteMessage.getData());
          }
  
      }
  
  
      public void startNotification(RemoteMessage remoteMessage){
          String title = remoteMessage.getData().get("title");
          String messsge = remoteMessage.getData().get("body");
          String type = remoteMessage.getData().get("type");
          String id = remoteMessage.getData().get("id");
  
  
          Log.i("Notification","active");
          //Log.i("Notificaition","type:"+type);
          //Log.i("Notificaition","id:"+id);
  
          //check if notification eihter is reminder or new quote/event
          if (!type.equals("reminder") &amp;&amp; id.equals("none")) {
  
              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);
  
              NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
              notificationBuilder.setContentTitle(title);
              notificationBuilder.setContentText(messsge);
              notificationBuilder.setSmallIcon(R.drawable.icon_no_bg);
              notificationBuilder.setAutoCancel(true);
              notificationBuilder.setContentIntent(pendingIntent);
  
              NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
              notificationManager.notify(0, notificationBuilder.build());
              //super.onMessageReceived(remoteMessage);
          }else
  
          if(type.equals("reminder") &amp;&amp; ! id.equals("none")) {
  
              Intent intent = new Intent(this, EventDetailsActivity.class);
              intent.putExtra("id",id);
              intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
              PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
  
              NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
              notificationBuilder.setContentTitle(title);
              notificationBuilder.setContentText(messsge);
              notificationBuilder.setSmallIcon(R.drawable.icon_logo_only);
              notificationBuilder.setAutoCancel(true);
              notificationBuilder.setContentIntent(pendingIntent);
  
              NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
              notificationManager.notify(0, notificationBuilder.build());
          }
  
      }
  }
  </code></pre>