2017-03-01 8 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 –

答えて

1

を使用して送信します

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

FCMは、通常、彼らが送信された直後にメッセージを配信します。しかし、これは必ずしも可能ではないかもしれません。たとえば、プラットフォームがAndroidの場合、デバイスはオフ、オフライン、またはその他の方法で使用できなくなる可能性があります。 FCMは意図的にメッセージを遅延させて、アプリが過度のリソースを消費するのを防ぎ、バッテリ寿命に悪影響を及ぼさないようにします。

と、この:

あなたがメッセージの最大寿命を指定するには、HTTPやXMPP要求の両方でサポートさtime_to_liveパラメータを、使用することができます。このパラメータの値は、0〜2,419,200秒の持続時間でなければならず、FCMがメッセージを格納して配信しようとする最大時間に対応します。このフィールドを含まないリクエストは、デフォルトで最大4週間の期間になります。

+0

を使用した場合はtime_to_liveパラメータを削除します.2419200を生きる時間を変更しました...今すぐ働きますThanks @Vyacheslav –

関連する問題