1

我只在應用程序處於後臺時收到推送通知,我找不到在應用程序接收推送時到底發生了什麼。我只想更改通知正文,例如,如果通知消息是「嗨」,我想要顯示用戶「嗨用戶」。在應用程序處於後臺時修改android中的firebase推送通知

public class MyFcmListenerService extends FirebaseMessagingService { 

    @Override 
    public void onMessageReceived(RemoteMessage message) { 
     //nothing triggered here when app is in background 
    } 

} 
+0

在後臺它會直接觸發,你不能改變消息。您需要在服務器端的通知有效負載中設置消息。當應用程序處於後臺時,您可以使用來自主要活動或操作活動的意向束來處理負載。 –

+0

查看[如何處理Firebase中後臺應用的通知](http://stackoverflow.com/a/37845174/3536264) –

+0

從哪裏發送通知?我的意思是從Firebase控制檯或你的服務器?如果您從Firebase發送信息,則在發送通知時,轉到高級選項並向通知添加數據。如果您從服務器發送,則不發送顯示消息(僅發送數據消息)。 –

回答

1
$fields = array(
'registration_ids' => $reg_id , 
'priority' => "high", 
'data' => array(******));  
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 

我都面臨着同樣的問題。從我的PHP Firebase Service中刪除了通知鍵值。我的問題得到解決。我只是用registration_idsprioritydata

0

可以,你只需要知道推送通知如何在Android火力點工作。

你需要重寫

handleIntent功能。

此函數在後臺處理Firebase通知。因此,在它內部,您將使您的推送通知以推送消息的形式發送所有數據。不要忘記從消息中提取信息。您可以使用標題或正文等默認空格,但也可以發送一些自定義數據。

接下來我會附上一個示例代碼,它是如何工作的。

注意:如果你還沒有這個方法,那麼你需要升級火力版本比起來10.0.1

public class MyFirebaseMessagingService extends FirebaseMessagingService { 
    private static final String TAG = "FCM Service"; 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     // TODO: Handle FCM messages here. 
     // If the application is in the foreground handle both data and notification messages here. 
     // Also if you intend on generating your own notifications as a result of a received FCM 
     // message, here is where that should be initiated. 
     Log.d(TAG, "From: " + remoteMessage.getFrom()); 
     Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 
    } 

    @Override 
    public void handleIntent(Intent intent) { 
     //super.handleIntent(intent); 

     Log.d(TAG,"Handling Intent"); 
     Bundle mBundle = intent.getExtras(); 
     String img = mBundle.getString("imgURL"); 
     String title = mBundle.getString("gcm.notification.title"); 
     String body = mBundle.getString("gcm.notification.body"); 
     mBundle.putInt("promoId",Integer.valueOf(mBundle.getString("promoId"))); 
     Integer id = mBundle.getInt("promoId"); 

     sendNotification(mBundle); 
    } 

    private void sendNotification(Bundle mBundle) { 
     // Create an explicit content Intent that starts the main Activity. 
     Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class); 

     notificationIntent.putExtras(mBundle); 

     // Construct a task stack. 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 

     // Add the main Activity to the task stack as the parent. 
     stackBuilder.addParentStack(MainActivity.class); 

     // Push the content Intent onto the stack. 
     stackBuilder.addNextIntent(notificationIntent); 

     // Get a PendingIntent containing the entire back stack. 
     PendingIntent notificationPendingIntent = 
       stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

     // Get a notification builder that's compatible with platform versions >= 4 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 

     String title = mBundle.getString("gcm.notification.title"); 
     String body = mBundle.getString("gcm.notification.body"); 

     // Define the notification settings. 
     builder.setSmallIcon(R.mipmap.ic_launcher) 
       // In a real app, you may want to use a library like Volley 
       // to decode the Bitmap. 
       .setLargeIcon(BitmapFactory.decodeResource(getResources(), 
         R.mipmap.ic_launcher)) 
       .setColor(Color.RED) 
       .setContentTitle(title) 
       .setContentText(body) 
       .setContentIntent(notificationPendingIntent); 

     // Dismiss notification once the user touches it. 
     builder.setAutoCancel(true); 

     // Get an instance of the Notification manager 
     NotificationManager mNotificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     // Issue the notification 
     mNotificationManager.notify(0, builder.build()); 
    } 

} 

,如果您有任何問題,請與我將編輯英里的答案。

相關問題