如何使用自定义布局显示Firebase通知?

4
Firebase有一个默认的简单通知布局,与Android的默认布局相同。如何将其更改为自定义布局,并在生成通知时显示通知。

在通知中加入图片或图标,请使用字母“u”。 - Dhruv Tyagi
创建自定义通知或者可以称之为折叠通知。 - PArth SOni
3个回答

1
FirebaseMessaging 服务中编写以下内容:
@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());
}

3
当应用程序在前台运行时,onMessageReceived方法会触发。 如果应用程序在后台运行,则不会触发。 请查看此答案:This Answer - Niamatullah Bakhshi
这种方法是唯一的方式,但只有在通过API使用数据有效载荷时才会始终触发。 - JoshuaTree
1
后台模式怎么样? - Alireza Noorali

0

需要记住的一件事是,如果您在 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);
    }

0

在您的服务器端,删除notification属性。
当您发送没有notification属性的通知时,Firebase将不会处理通知。 然后,您可以扩展FirebaseMessagingService来处理通知。

不要忘记在清单中注册服务。


1
我正在收到通知,但我想用自定义布局来更改它,该如何处理? - PArth SOni

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