当应用处于后台时,修改安卓中的Firebase推送通知



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

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

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

您需要覆盖

句柄意图函数。

此函数在后台处理 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());
    }
}

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

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

我遇到了同样的问题。从我的 PHP Firebase 服务中删除了通知键值。我的问题得到了解决。我只是在用registration_idsprioritydata

相关内容

  • 没有找到相关文章

最新更新