2016-10-05 7 views
0

AndroidのFirebaseを使用してサーバーから通知を送信しています。私のコードは、Firebaseコンソールから通知を送信するときに機能しますが、サーバからメッセージを送信すると機能しません。私はデバッグをオンにし、FirebaseMessagingServiceのonMessageReceivedメソッドが呼び出されないことを分析しました。ここで AndroidでFirebaseを使用しているサーバからの通知でonMessageReceivedが通知されない

は私FirebaseMessagingServiceコードです:ここで
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService { 
    private static final String TAG = FirebaseMessagingService.class.getSimpleName(); 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 

     showNotification(remoteMessage.getData().get("message")); 
    } 

    private void showNotification(String message){ 

      Intent intent = new Intent(this, MainActivity.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
      Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.ic_launcher) 
        .setContentTitle("Firebase Push Notification") 
        .setContentText(message) 
        .setAutoCancel(true) 
        .setSound(defaultSoundUri) 
        .setContentIntent(pendingIntent); 
      NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
      notificationManager.notify(0, notificationBuilder.build()); 
     } 
    } 

は、サーバ側」の私のPHPコードである

public function sendPushNotificationToGCMSever($token, $message){ 

     //include_once 'config.php'; 

     $url = 'https://fcm.googleapis.com/fcm/send'; 

     $fields = array(
       'registration_ids' => $token, 
       'notification' => $message 
     ); 

     $headers = array(
       'Authorization:key=' . SERVER_KEY, 
       'Content-Type:application/json' 
     ); 
     $ch = curl_init(); 

     $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, json_encode($fields)); 

     $result = curl_exec($ch); 

     curl_close($ch); 

     return $result; 
    } 

誰も私が間違ってやっているものを私に言うことができるすべてのヘルプははるかに高く評価されるだろう? 。ありがとうございました:)

+0

は、フォアグラウンドまたはバックグラウンドの状態でアプリですか? –

答えて

0

guideは、(コンソールのように通知を送信する)あなたの$fieldsは($messagein this blog post

を説明するようにもまた、ガイドのデータのみのメッセージや通知の違いを参照してください'icon' => 'the name of some file in res/drawables'を設定することにより、通知のアイコンを選択することができます

$fields = array(
       'to' => $token, 
       'notification' => array(
         'body' => $message 
       ), 
     ); 

)文字列です。

あなたはデータのみのメッセージとonMessageReceivedをしようとすると、あなたはおそらく(guideで述べたように)追加するのを忘れ

<service 
    android:name=".MyFirebaseMessagingService"> 
    <intent-filter> 
     <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
    </intent-filter> 
</service> 
AndroidManifest.xml 
+1

これはデータメッセージを送信し、コンソールは常に通知メッセージを送信します。 https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messagesを参照してください。 –

+0

私はあなたが書いたのと同じように試しましたが、運はありませんでした。 –

関連する問題