2017-03-01 114 views
0

當我的手機處於離線狀態時(未連接到互聯網),我向設備發送通知.5或6分鐘後,我將手機連接到互聯網,但沒有收到我以前發送的通知。如何在連接到互聯網後獲得通知。不顯示FCM通知

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMsgService"; 

    Integer notify_no = 0; 

    /** 
    * Called when message is received. 
    * 
    * @param remoteMessage Object representing the message received from Firebase Cloud Messaging. 
    */ 
    // [START receive_message] 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     // [START_EXCLUDE] 
     // There are two types of messages data messages and notification messages. Data messages are handled 
     // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type 
     // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app 
     // is in the foreground. When the app is in the background an automatically generated notification is displayed. 
     // When the user taps on the notification they are returned to the app. Messages containing both notification 
     // and data payloads are treated as notification messages. The Firebase console always sends notification 
     // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options 
     // [END_EXCLUDE] 

     // TODO(developer): Handle FCM messages here. 

     Log.d(TAG, "From: " + remoteMessage.getFrom()); 

     // Check if message contains a data payload. 
     if (remoteMessage.getData().size() > 0) { 
      Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
      /* Integer badge = Integer.parseInt(remoteMessage.getData().get("badge")); 
      Log.d("notificationNUmber",":"+badge); 
      setBadge(getApplicationContext(), badge);*/ 
     } 

     // Check if message contains a notification payload. 
     if (remoteMessage.getNotification() != null) { 
      Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
     } 
     notify_no = Integer.parseInt(remoteMessage.getData().get("msgcnt")); 

     if (notify_no < 1) { 
      notify_no = notify_no + 1; 
     } else { 
      notify_no = 0; 
     } 





     //EventBus.getDefault().post(remoteMessage.getNotification().getBody()); 
     sendNotification(remoteMessage.getNotification().getBody()); 



     // Also if you intend on generating your own notifications as a result of a received FCM 
     // message, here is where that should be initiated. See sendNotification method below. 
    } 




public void sendNotification(String messageBody) 
{ 

    Intent intent = new Intent(this,Main2Activity.class); 
     intent.putExtra("messages","messages"); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     intent.putExtra("fcm_notification", "Y"); 
     // startActivity(intent); 

     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) 
       .setContentTitle("FCM Message") 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(Uri.parse("content://settings/system/notification_sound")) 
       .setVibrate(new long []{100,2000,500,2000}) 
       .setContentIntent(pendingIntent); 

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


     if (NOTIFICATION_ID > 1073741824) { 
      NOTIFICATION_ID = 0; 
     } 
     notificationManager.notify(NOTIFICATION_ID++, notificationBuilder.build()); 


     // ShortcutBadger.with(getApplicationContext()).count(badgeCount); 
    } 
} 

如何在連接到互聯網後獲取通知?

我發送使用PHP

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); 
$result = curl_exec($ch); 
if ($result === FALSE) { 
die('Curl failed: ' . curl_error($ch)); 
} 
curl_close($ch); 
+0

如何發送通知?服務器端還是Firebase控制檯? –

+0

它將盡快交付 – Vyacheslav

+0

@Divyesh我發送使用php –

回答

1

正如在手冊中看到:

https://firebase.google.com/docs/cloud-messaging/concept-options

FCM通常提供的消息在發送後即可。但是,這可能不總是可能的。例如,如果平臺是Android,則該設備可能被關閉,脫機或不可用。 FCM可能會故意拖延消息,以防止應用程序消耗過多的資源並對電池壽命產生負面影響。

這:

可以使用time_to_live參數,在HTTP和XMPP請求支持,指定一個消息的最大壽命。該參數的值必須是從0到2419200秒的持續時間,並且它對應於FCM存儲和嘗試傳遞消息的最長時間段。不包含此字段的請求默認爲最多四周。

+0

我改變了生活的時間2419200 ...現在它的工作謝謝@Vyacheslav –