2017-12-27 259 views
2

的Android火力地堡背景通知我開發一個Android應用程序接收來自火力地堡推送通知。沒有發射

我可以得到令牌並從郵差發送推送通知沒有任何問題。

如果應用程序在前臺,一切都按預期工作,並且我在onMessageReceived(我用各種有效載荷進行測試)中收到有效載荷。

但如果我關閉應用程序也沒有收到什麼。我嘗試了很多有效載荷,和我讀(有效載荷數據和通知之間的性差異)中的所有文檔。

這裏是我的項目使用我的班:

1 - 這是負責獲得令牌

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { 

private static String TAG = "Android Push App"; 

@Override 
public void onTokenRefresh() { 
    String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
    sendRegistrationToServer(refreshedToken); 
} 

private void sendRegistrationToServer(String token) { 
    Log.d(TAG, "Did obtained token"); 
    Log.d(TAG, token); 
} 

3類 - 的擴展FirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

private static final String TAG = "Android Push App"; 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    sendNotification("Received notification"); 
} 

private void sendNotification(String messageBody) { 
    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 = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.push_icon) 
        .setContentTitle("FCM Message") 
        .setContentText(messageBody) 
        .setAutoCancel(true) 
        .setSound(defaultSoundUri) 
        .setContentIntent(pendingIntent); 

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

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

2類 - 我的清單:

<uses-permission android:name="android.permission.INTERNET" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <action android:name="FIREBASE_ACTIVITY" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 

    <service android:name=".push.core.MyFirebaseInstanceIDService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
     </intent-filter> 
    </service> 

    <service 
     android:name=".push.core.MyFirebaseMessagingService" 
     android:enabled="true" 
     android:exported="true"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
     </intent-filter> 
    </service> 

    <meta-data 
     android:name="com.google.firebase.messaging.default_notification_icon" 
     android:resource="@drawable/push_icon" /> 

    <meta-data android:name="com.google.firebase.messaging.default_notification_color" 
     android:resource="@color/colorAccent" /> 

</application> 

4 - 在MainActivity

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    if (getIntent().getExtras() != null) { 
     for (String key : getIntent().getExtras().keySet()) { 
      Object value = getIntent().getExtras().get(key); 
      Log.d("MainActivity", "Key: " + key + " Value: " + value); 
     } 
    } 
} 

5 - 最後,我想這有效載荷在郵差

POST https://fcm.googleapis.com/fcm/send 內容類型的應用程序/ JSON 授權密鑰= AIzaSy(...)KY

JSON體(I試過示例):

{ 
"to": "dxe0RDKbP...m9Uc","notification" : { 
    "body" : "great match!", 
    "title" : "Portugal vs. Denmark", 
    "icon" : "push_icon", 
    "sound" : "default" 
}} 

{ 
"to": "dxe0RDKbP...m9Uc","notification" : { 
    "body" : "great match!", 
    "title" : "Portugal vs. Denmark", 
    "icon" : "push_icon", 
    "sound" : "default" 
}} 

和:

{ 
"to": "d3j-9OJ6R...C6w", 
"notification" : { 
    "title": "title", 
    "body": "body" 
}, 
"data": { 
    "tipo": "normal" 
}} 

還增加了 「優先」 鍵,這是行不通的。

我做錯了什麼?

感謝所有幫助,您可以給我:)

UPDATE

現在,它的工作。

FireBaseMessagingService與正在運行的Geofence Push(由App啓動)之間存在衝突。

刪除該地理柵欄服務一切正常後。

還使用通知數據按鍵的有效載荷中。

感謝

回答

1

試試這個

你不能把JSON關鍵'notification'在您的請求火力API,而是使用'data'

使用本

{ 
    "to": "dxe0RDKbP...m9Uc", 
    "data": { 
    "body" : "great match!", 
    "title" : "Portugal vs. Denmark", 
    "icon" : "push_icon", 
    "sound" : "default" 
    } 
} 

編輯 你可以只用身體和標題嘗試

{ 
    "to": "dxe0RDKbP...m9Uc", 
    "data": { 
     "body" : "great match!", 
     "title" : "Portugal vs. Denmark" 
    } 
} 

編輯新建

清單檔案中的android:stopWithTask="false"服務屬性添加這一點。

<service 
     android:name="com.yourapp.YourPushService" 
     android:stopWithTask="false" 
     android:enabled="true" 
     android:exported="true"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
     </intent-filter> 
</service> 
+0

Stil不工作。我曾嘗試過這種有效載荷。可以是關於圖標或聲音的東西嗎? –

+0

@MichaelChaves請檢查我的編輯答案。並告訴我這是什麼結果。 –

+0

如果應用程序處於前景或背景中,它可以正常工作。如果我殺應用程序沒有任何反應 –

0

您需要從json中刪除通知負載並向其添加數據負載。這是因爲Android已經採取的通知護理的內置功能,當它看到通知的有效載荷,即其每當通知有效載荷發送機器人直接將其發送到系統板車和onMessageReceived功能,不叫。

0

具體根據火力文檔,火力地堡通知行爲不同根據接收的應用程序的前景/背景狀態。

onMessageReceived提供了大多數的消息類型,但下列情況除外:當你的應用程序在後臺交付

通知消息。在這種情況下,通知將傳遞到設備的系統托盤。用戶點擊通知會默認打開應用程序啓動器。

消息使用二者通知和數據有效載荷,無論背景和前景。在這種情況下,通知將傳遞到設備的系統托盤,並且數據有效負載將以您啓動程序活動的目的附加內容傳遞。

所以,你需要從你的JSON刪除通知的有效載荷,只需要保存的數據有效載荷來觸發onMessageReceived當應用程序是在後臺。

鏈接:https://firebase.google.com/docs/cloud-messaging/android/receive