4

我似乎無法接收FCM推送通知,這是我在Android上終止應用程序後從FCM控制檯發送的,因爲在長按「概覽」按鈕並輕掃應用程序以終止。當應用程序在前臺或後臺運行時,它工作得很好。這可能看起來像一個重複的問題,但我嘗試了其他方法,但我仍然無法得到它。應用程序死亡後的推送通知

NotificationService.java

public class NotificationService extends FirebaseMessagingService 
{ 

private static final String TAG = "FCM Service"; 
@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 

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

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, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     android.support.v4.app.NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle("Firebase") 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

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

     notificationManager.notify(0, notificationBuilder.build()); 
} 
} 

TokenRefresh.java

public class TokenRefresh extends FirebaseInstanceIdService { 

private static final String TAG = "FirebaseIDService"; 

@Override 
public void onTokenRefresh() { 
    // Get updated InstanceID token. 
    String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
    Log.d(TAG, "Refreshed token: " + refreshedToken); 

    // TODO: Implement this method to send any registration to your app's servers. 
    sendRegistrationToServer(refreshedToken); 
} 

/** 
* Persist token to third-party servers. 
* 
* Modify this method to associate the user's FCM InstanceID token with any server-side account 
* maintained by your application. 
* 
* @param token The new token. 
*/ 
private void sendRegistrationToServer(String token) { 
    // Add custom implementation, as needed. 

} 

} 

MainActivity.java

public class MainActivity extends AppCompatActivity { 

Context context; 

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


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

    String token = ("fcm"+ FirebaseInstanceId.getInstance().getToken()); 


} 

} 
+0

查看下面鏈接中的第一個答案;-) http://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase玩得開心 –

+0

@ AL。我嘗試從POST發送數據消息,但是當我將它發送到我的應用程序時,應用程序崩潰。我可以通過POST正常接收通知,而不是通過POST發送數據消息,但是沒有數據消息,我的代碼是否有問題或者清單中接收數據消息需要額外的東西? – Tyson

+0

你可以發佈堆棧跟蹤嗎? –

回答

0

嘗試換旗

Intent intent = new Intent(this, MainActivity.class); 
    intent.addFlags(Intent.FLAG_FROM_BACKGROUND); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
      PendingIntent.FLAG_ONE_SHOT); 
+0

更改標誌對此問題無效。 – Tyson

+0

這有什麼用?這些是來自FCM服務器的通知,而不是您的應用正在生成的內容。沒有機會創造一個意圖。 –

0

嘗試增加BroadcastReceiverMainActivity.java

BroadcastReceiver mRegistrationBroadcastReceiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 

       // checking for type intent filter 
       if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) { 
        // gcm successfully registered 
        // now subscribe to `global` topic to receive app wide notifications 
        FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL); 

        displayFirebaseRegId(); 

       } else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) { 
        // new push notification is received 

        String message = intent.getStringExtra("message"); 

        Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show(); 

        txtMessage.setText(message); 
       } 
      } 
     }; 

     displayFirebaseRegId(); 
    } 

    // Fetches reg id from shared preferences 
    // and displays on the screen 
    private void displayFirebaseRegId() { 
     SharedPreferences pref = getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0); 
     String regId = pref.getString("regId", null); 

     Log.e(TAG, "Firebase reg id: " + regId); 

     if (!TextUtils.isEmpty(regId)) 
      txtRegId.setText("Firebase Reg Id: " + regId); 
     else 
      txtRegId.setText("Firebase Reg Id is not received yet!"); 
    } 

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

     // register GCM registration complete receiver 
     LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver, 
       new IntentFilter(Config.REGISTRATION_COMPLETE)); 

     // register new push message receiver 
     // by doing this, the activity will be notified each time a new message arrives 
     LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver, 
       new IntentFilter(Config.PUSH_NOTIFICATION)); 

     // clear the notification area when the app is opened 
     NotificationUtils.clearNotifications(getApplicationContext()); 
    } 

    @Override 
    protected void onPause() { 
     LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver); 
     super.onPause(); 
    } 
} 
+0

但是,如何在Firebase已運行通知服務時專門爲其啓動服務? – Tyson

+0

如果您添加SDK,是否不處理該服務?或者我錯了?@smeet – Tyson

+0

我已經在我的清單中有這兩個服務: 'code' <服務機器人:名字=」。NotificationService 「> <意圖過濾器> <操作機器人:名字=」 com.google.firebase .MESSAGING_EVENT 「/> <服務機器人:名稱=」。TokenRefresh 「> <意圖濾波器> <操作機器人:名稱=」 com.google.firebase.INSTANCE_ID_EVENT」/> 'code' 我還遺失了什麼? – Tyson

0

我有完全相同的問題。通知在前景和背景模式下顯示得很好,但在應用程序被殺時不顯示。最終我發現這個:https://github.com/firebase/quickstart-android/issues/41#issuecomment-306066751

問題在於Android Studio中的調試模式。要正確測試您的通知,請執行以下操作: 在Android Studio中通過調試運行您的應用程序。 將其滑開即可被殺死。 通過手機上的啓動器圖標重新啓動應用程序。 再次滑開它(所以它被殺死)。

發送您的通知,現在你會看到它!

希望這解決了您的問題。