当Android应用程序在后台运行时,修改Firebase推送通知

3

我的应用程序只有在后台运行时才能收到推送通知,我无法找到是什么触发了我的应用程序接收推送。我只想更改通知正文,例如,如果通知消息是“hi”,我想向用户显示“hi user”。

public class MyFcmListenerService extends FirebaseMessagingService {

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

}

在后台,它将直接触发,您无法更改消息。您需要在服务器端的通知负载中设置消息。当应用程序在后台时,您可以使用意图捆绑包从主活动或操作活动处理负载。 - Jd Prajapati
你是从哪里发送通知的?我的意思是从 Firebase 控制台还是你的服务器?如果你是从 Firebase 发送通知,在发送通知时,进入高级选项并将数据添加到通知中。如果你是从服务器发送,请不要发送显示消息(只发送数据消息)。 - Faraz
2个回答

3
你只需要了解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());
    }

}

如果您有任何问题,请问我并且我会编辑我的回答。

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

我曾经遇到过相同的问题。我从我的PHP Firebase服务中删除了通知键值,问题得到了解决。我只是使用registration_idsprioritydata

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接