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()); 
    } 

} 

,如果您有任何问题,请与我将编辑英里的答案。

相关问题