Firebase有一个默认的简单通知布局,与Android的默认布局相同。如何将其更改为自定义布局,并在生成通知时显示通知。
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
try {
JSONObject jsonObject = new JSONObject(remoteMessage.getData());
Log.e("Tag",remoteMessage.getData().toString());
sendNotification(remoteMessage.getData().toString());
} catch (Exception e) {
}
}
private void sendNotification(String msg) {
Intent intent = new Intent(this, NewTransactionsHistActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, new Random().nextInt(100) , intent,
PendingIntent.FLAG_ONE_SHOT);
long when = System.currentTimeMillis();
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this);
mNotifyBuilder.setVibrate(new long[] { 1000, 1000,1000,1000,1000,1000});
boolean lollipop = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
if (lollipop) {
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.app_name))
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setColor(Color.TRANSPARENT)
.setLargeIcon(
BitmapFactory.decodeResource(
getResources(),
R.drawable.rlogo))
.setSmallIcon(R.drawable.ic_icon_lollipop)
.setWhen(when).setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
} else {
mNotifyBuilder = new NotificationCompat.Builder(this)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentTitle(getString(R.string.app_name)).setContentText(msg)
.setSmallIcon(R.drawable.rlogo)
.setWhen(when).setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(new Random().nextInt(100) /* ID of notification */, mNotifyBuilder.build());
}
需要记住的一件事是,如果您在 Firebase 负载上没有添加数据块... 您的 onMessageReceived 方法将不会被调用。
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
**FROM HERE TO COPY**
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
RunNotification(title,body);
**METHOD**
private void RunNotification(String title, String messageBody) {
RemoteViews contentView;
Notification notification;
NotificationManager notificationManager;
int NotificationID = 1005;
NotificationCompat.Builder mBuilder;
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(getApplicationContext(), "notify_001");
contentView = new RemoteViews(getPackageName(), R.layout.notification);
contentView.setImageViewResource(R.id.image, R.drawable.img);
contentView.setTextViewText(R.id.title, title);
contentView.setTextViewText(R.id.text, messageBody);
mBuilder.setSmallIcon(R.drawable.ic_baseline_location_on_24);
mBuilder.setAutoCancel(false);
mBuilder.setContentTitle(title);
mBuilder.setContentText(messageBody);
mBuilder.setPriority(Notification.PRIORITY_HIGH);
mBuilder.setOnlyAlertOnce(true);
mBuilder.build().flags = Notification.FLAG_NO_CLEAR | Notification.PRIORITY_HIGH;
mBuilder.setContent(contentView);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "channel_id";
NotificationChannel channel = new NotificationChannel(channelId, "channel name", NotificationManager.IMPORTANCE_HIGH);
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(channel);
mBuilder.setChannelId(channelId);
}
notification = mBuilder.build();
notificationManager.notify(NotificationID, notification);
}
在您的服务器端,删除notification
属性。
当您发送没有notification
属性的通知时,Firebase将不会处理通知。
然后,您可以扩展FirebaseMessagingService
来处理通知。
不要忘记在清单中注册服务。