【问题标题】:Modify firebase push notification in android when app is in background当应用程序在后台时修改android中的firebase推送通知
【发布时间】:2017-04-06 11:54:29
【问题描述】:

我只在应用程序处于后台时收到推送通知,当我的应用程序接收推送时,我找不到确切触发的内容。我只想更改通知正文,例如,如果通知消息是“hi”,我想向用户显示“hi user”。

public class MyFcmListenerService extends FirebaseMessagingService {

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

}

【问题讨论】:

  • 后台直接触发,无法更改消息。您需要在服务器端的通知负载中设置消息。当应用程序处于后台时,您可以使用 mainactivity 或 action 活动中的意图包处理有效负载。
  • 您从哪里发送通知?我的意思是来自Firebase 控制台或您的服务器?如果您从 Firebase 发送通知,请在发送通知时转到高级选项并将数据添加到通知中。如果您从服务器发送,请不要发送显示消息(仅发送数据消息)。

标签: android firebase push-notification firebase-cloud-messaging


【解决方案1】:
$fields = array(
'registration_ids' => $reg_id ,
'priority' => "high",
'data' => array(******));    
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

我也遇到过同样的问题。从我的 PHP firebase 服务中删除了通知键值。我的问题得到了解决。我只是使用registration_idspriority, data

【讨论】:

    【解决方案2】:

    你可以,你只需要知道 Firebase 推送通知在 android 中是如何工作的。

    你需要重写

    handleIntent 函数。

    此函数在后台处理 Firebase 通知。所以在它里面你会让你的推送通知接收推送消息中发送的所有数据。不要忘记从消息中提取信息。您可以使用标题或正文等默认空格,也可以发送一些自定义数据。

    接下来我会附上一个示例代码它是如何工作的。

    注意:如果您没有此方法,则需要将 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());
        }
    
    }
    

    如果您有任何问题,我会编辑我的答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-02
      • 1970-01-01
      相关资源
      最近更新 更多