2017-09-19 11 views
0

firebaseクラウドメッセージングサービスを使用して簡単なメッセージングアプリケーションを作成していますが、クラウド機能を使用して通知を処理しています。ログが、デバイスがここで何もログに正常に送信されたにもかかわらず通知を受信しないデバイス

を受けていないが使用され、クラウド機能である:ここで

exports.sendNotifications = functions.database.ref('/meesages/{messageId}').onCreate(event => { 
    var eventSnapshot = event.data; 
    var str1 = "Sender : "; 
    var str = str1.concat(eventSnapshot.child("messageOwner").val()); 
    console.log(str); 

    var topic = "Messaging"; 
    var payload = { 
     notification: { 
      Message: eventSnapshot.child("messageText").val(), 
      Sender: eventSnapshot.child("messageOwner").val() 
     } 
    }; 

    // Send a message to devices subscribed to the provided topic. 
    return admin.messaging().sendToTopic(topic,payload) 
     .then(function (response) { 
      // See the MessagingTopicResponse reference documentation for the 
      // contents of response. 
      console.log("Successfully sent message:", response); 
     }) 
     .catch(function (error) { 
      console.log("Error sending message:", error); 
     }); 

}); 

は、Androidデバイス上で通知部分の処理を担当するクラスです:

@Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     // Check if message contains a data payload. 
     if (remoteMessage.getData().size() > 0) { 
      showNotification(remoteMessage.getData().get("Sender"), remoteMessage.getData().get("Message")); 
     } 

     // Check if message contains a notification payload. 
     if (remoteMessage.getNotification() != null) { 

      Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 

     } 

    } 

    @Override 
    public void onDeletedMessages() { 
     super.onDeletedMessages(); 
    } 

    private void showNotification(String Message, String Sender) { 
     Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
       .setContentTitle("New message : " + Message) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentText("By : " + Sender) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

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

     notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
    } 
} 

とはい私はそう何がここに正確に間違っているmainactivityクラスに

FirebaseMessaging.getInstance().subscribeToTopic("Messaging"); 

をトピックサブスクリプションを追加することを確認しました?

+0

をあなたはマニフェストファイルでサービスを定義しましたか? – Kuls

+0

はまだ実行中のデバイスサービスですか? – AndroSco

+0

はいマニフェスト でサービスを最初に定義しましたが、デバイスサービスがまだ実行中でした –

答えて

0

onMessageReceived()のコードでは、メッセージにデータペイロードが必要です。これは、the documentationで説明されています。このタブには、通知、データ、および結合ペイロードを示す3つのタブがあります。 notificationdataに変更します。

var payload = { 
     data: { 
      Message: eventSnapshot.child("messageText").val(), 
      Sender: eventSnapshot.child("messageOwner").val() 
     } 
    }; 
+0

そのまま4時間のように過ごしました 私は感謝できませんあなたは十分です –

関連する問題