firebase通知显示为文本视图



当用户单击通知栏上时,如何使即将到来的firebase通知消息机构和消息标题显示为文本视图?

您需要做的就是处理FirbaseMessagingService类中接收的通知。它具有onMessageReceived()方法,该方法应该完成您的工作。请参阅下面的示例:

    import android.app.Notification;
    import android.app.NotificationChannel;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Color;
    import android.media.RingtoneManager;
    import android.net.Uri;
    import android.os.Build;
    import android.os.Bundle;
    import android.support.v4.app.NotificationCompat;
    import android.util.Log;
    import com.google.firebase.messaging.FirebaseMessagingService;
    import com.google.firebase.messaging.RemoteMessage;
    import java.io.IOException;
    import java.net.URL;
    import java.util.Map;
    import java.util.concurrent.atomic.AtomicInteger;
    /**
     * Created by nileshdeokar on 12/02/2018.
     */
    public class PQFIrebaseMessagingService extends FirebaseMessagingService {
        public static final String FCM_PARAM_TEXT = "text";
        public static final String FCM_PARAM_TEXT_BODY = "body";
        public static final String NOTIFICATION_CHANNEL_ID = "IncomingMessages";
        public static final String CHANNEL_NAME = "Message";
        public static final String IMAGE_URL = "picture";
        private int numMessages = 0;
        private final static AtomicInteger c = new AtomicInteger(0);
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            super.onMessageReceived(remoteMessage);
            RemoteMessage.Notification notification = remoteMessage.getNotification();
            Map<String, String> data = remoteMessage.getData();
            Log.d("FROM", remoteMessage.getFrom());
            sendNotification(notification, data);
        }
        private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {
            Bundle bundle = new Bundle();
            String text = data.get(FCM_PARAM_TEXT);
            String body = data.get(FCM_PARAM_TEXT_BODY);
            if (text != null && !text.isEmpty()) {
                bundle.putString(FCM_PARAM_TEXT, text);
            }
            if (body != null && !body.isEmpty()) {
                bundle.putString(FCM_PARAM_TEXT_BODY, body);
            }
            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtras(bundle);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID)
                    .setContentTitle(notification.getTitle())
                    .setContentText(notification.getBody())
                    .setAutoCancel(true)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setSound(Uri.parse("android.resource://" + getPackageName() + "/" +  R.raw.msg_ring))
                    .setContentIntent(pendingIntent)
                    .setContentInfo("Hello")
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                    .setColor(getColor(R.color.colorAccent))
                    .setLights(Color.RED, 1000, 300)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setDefaults(Notification.DEFAULT_VIBRATE)
                    .setVibrate(new long[]{100, 200, 300, 400, 500})
                    .setNumber(++numMessages)
                    .setSmallIcon(R.drawable.ic_like);
            try {
                String picture = data.get(IMAGE_URL);
                if (picture != null && !"".equals(picture)) {
                    URL url = new URL(picture);
                    Bitmap bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                    notificationBuilder.setStyle(
                            new NotificationCompat.BigPictureStyle().bigPicture(bigPicture).setSummaryText(notification.getBody())
                    );
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(
                        NOTIFICATION_CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT
                );
                channel.setDescription("description");
                channel.setShowBadge(true);
                channel.canShowBadge();
                channel.enableLights(true);
                channel.setLightColor(Color.RED);
                channel.enableVibration(true);
                channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500});
                assert notificationManager != null;
                notificationManager.createNotificationChannel(channel);
            }

            assert notificationManager != null;
            notificationManager.notify(getID(), notificationBuilder.build());
        }

        public static int getID() {
            return c.incrementAndGet();
        }
    }

然后在MainActivity中您需要做的就是:

String receivedText = getIntent().getStringExtra(FCM_PARAM_TEXT);
String receivedBody = getIntent().getStringExtra(FCM_PARAM_TEXT_BODY);

您可以在单击通知时根据您修改firebase消息主体和消息,因为您可以将firbasemessagingservice class的onMessageCeive((方法

相关内容

  • 没有找到相关文章

最新更新