当用户点击通知栏时,如何让即将到来的Firebase Notification messageBody和消息Title在mainActivity中显示为textView?
答案 0 :(得分:1)
您需要做的就是在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);
答案 1 :(得分:0)
您可以点击通知修改Firebase邮件正文和邮件,因为它会将您带到FirbaseMessagingService类的onMessageReceived()方法