2016-12-02 70 views
2

我連一個Android應用以下this guide谷歌雲火力地堡的消息服務(FCM),應用程序連接到FCM沒有從AWS收到通知SNS

而且我是按照this answer設置FCM & AWS SNS之間的連接。

我能夠成功地接收來自FCM console發送消息,但沒有從AWS SNS console

登錄AWS消息delivery status表現出對每次我雖然沒有被通知我的設備上顯示消息發送成功。

有沒有辦法檢查發生了什麼?

+0

同樣在這裏。沒有什麼地方可以找到消息如何以及何時下降。 Cloudwatch報告顯示我已發送的每封郵件均已成功,但沒有任何郵件已發送到設備。有沒有人在那裏回答這個問題。我懷疑Firebase在這裏。 –

回答

1

這裏的問題在於AWS SNS發送了Google調用的數據消息。

通過FCM,您可以發送兩種類型的消息 - 通知和數據。 FCM自動顯示通知,而數據消息則不顯示。更多信息請訪問:https://firebase.google.com/docs/cloud-messaging/concept-options

通過擴展FirebaseMessagingService並覆蓋它的方法,即使您的應用在後臺,仍然可以處理來自SNS的數據消息。更多關於此這裏:https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService

我想你想你的AWS SNS的消息來模仿通知的經驗,即:

  • 看到他們時彈出應用程序在後臺
  • 有你的文字顯示通知
  • 當應用程序激活你希望所有從 抽屜清出來的消息

乙酰膽鹼即使這樣,你會想做三件事。

首先 - 如果您的應用程序當前是可見的,您需要開始跟蹤。如何可靠地檢測這一細節,你可以在這裏找到:https://stackoverflow.com/a/18469643/96911

其次 - 你要通過發佈通知,以處理來自AWS SNS數據信息,但只有當你的應用程序在後臺:

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    static protected int id = 0; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     super.onMessageReceived(remoteMessage); 

     if (!MyApplication.isActivityVisible()) { 
      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); 
      mBuilder.setContentTitle(getString(R.string.app_name)) 
        .setSmallIcon(R.drawable.notification_icon); 

      String message = remoteMessage.getData().get("default"); 
      mBuilder.setContentText(message); 

      Intent resultIntent = new Intent(this, MainActivity.class); 
      PendingIntent resultPendingIntent = 
        PendingIntent.getActivity(
          this, 
          0, 
          resultIntent, 
          PendingIntent.FLAG_UPDATE_CURRENT 
        ); 
      mBuilder.setContentIntent(resultPendingIntent); 

      NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

      mNotificationManager.notify(id ++, mBuilder.build()); 
     } 
    } 

} 

最後 - 當用戶點擊其中一個時,您需要清除抽屜中的所有通知。與我聯繫只是響應該通知應具有以下onResume方法的活動之上的知名度跟蹤相結合:

@Override 
protected void onResume() { 
    super.onResume(); 

    MyApplication.activityResumed(); 

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

    mNotificationManager.cancelAll(); 
} 

這是一個很長的時間,因爲你問這個問題,但它是如此痛苦,我去無論如何,我決定回答這個問題。我希望這可以幫助你或者別人甩掉他們的頭髮,試圖讓這件事情起作用(因爲使iOS工作變得輕而易舉,謝謝)。

0

爲了充分利用AWS SNS控制檯中的數據按照下面的步驟:

1)添加項目FCM和使用傳統的服務器鍵AWS SNS。

2)通過使用下面的代碼把設備的令牌:

String deviceToken = FirebaseInstanceId.getInstance().getToken(); 

3)執行下面的代碼在應用程序中

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { 

@Override 
public void onTokenRefresh() { 

    String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
    sendRegistrationToServer(refreshedToken); 
} 

private void sendRegistrationToServer(String token) { 
    // TODO: Implement this method to send token to your app server. 
} 

}

4)重寫onMessageReceived()其在接到通知時調用:

public class AppFirebaseMessagingService extends FirebaseMessagingService { 
static protected int id = 0; 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 

    //remoteMessage.getNotification().getBody() 


    if (remoteMessage.getData().get("default").length() > 0) { 
       Intent intent = new Intent(this, MainActivity.class); 
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); 
     Uri ringNotificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle("ApplicationName") 
       .setContentText(remoteMessage.getData().get("default")) 
       .setAutoCancel(true) 
       .setSound(ringNotificationSound) 
       .setContentIntent(pendingIntent); 
     NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(id++, notifyBuilder.build()); 

    } 
} 

}

當我們從AWS SNS服務獲得通知時,我們使用remoteMessage.getData().get("default")來閱讀來自AWS的消息。

2

您可以使用此視頻教程https://youtu.be/iBTFLu30dSg以及有關如何使用FCM和AWS SNS的英文字幕一步一步以及如何從AWS控制檯發送推送通知的示例。它適用於我,並且我成功地收到了SNS控制檯和移動設備上的我的代碼的推送通知。

相關問題