【问题标题】:how to show firebase notification with custom layout?如何使用自定义布局显示 Firebase 通知?
【发布时间】:2017-05-05 07:29:46
【问题描述】:

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

【问题讨论】:

  • u 表示在通知中添加图像或图标
  • 没有兄弟创建自定义通知或可以说折叠通知。

标签: android firebase push-notification android-notifications firebase-cloud-messaging


【解决方案1】:

FirebaseMessaging service 中写入以下内容:

@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());
}

【讨论】:

  • onMessageReceived 方法在应用程序处于前台时触发。如果应用程序在后台运行,它不会触发。检查This Answer
  • 该方法是唯一的方法,但只有在通过API使用数据负载时才会一直触发。
  • 后台模式呢?
【解决方案2】:

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

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

【讨论】:

  • 我收到通知,但我想用自定义布局更改它如何处理它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-27
  • 1970-01-01
相关资源
最近更新 更多