android - 使用 FireBase 的 Android In App 通知显示
问题描述
希望我的措辞正确,但 Android 是否提供标准机制(例如弹出窗口或横幅)来在应用程序内部显示通知?通知将通过 FireBase 发送到设备,一旦我在 FireBase 服务 onMessageReceived() 中收到通知,我想在应用程序内显示通知,因此用户可以让通知消失或对其做出反应(将它们带到应用程序的其他位置)。
因此,在 Firebase 消息传递服务中,我想根据传入的数据对通知做出反应(我知道这仅适用于前台的应用程序,但我已经实现了其他代码来处理系统托盘的 Intent 中的它) :
public class MyCompanyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
// Do a popup or something to alert the user and then allow them to move to that activity
}
}
在 Android 中是否有模式或标准方法可以做到这一点,还是我需要自己编写代码?我似乎在 Android 文档中找不到任何对它的引用。
此外,这与通知渠道有何关系?
解决方案
请参阅此处的示例:https ://demonuts.com/firebase-cloud-messaging-android/
按照private void generateNotification(String messageBody)
帖子里的方法。
从 Android O 开始,需要在通知通道中发布通知。通知通道有助于对我们的应用发送的通知进行分组。
因此,在上面的帖子中,您NotificationCompat.Builder
只使用调用方法创建通知setChannel()
并设置创建的频道。高级步骤将是-
创建通知通道:
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); String channelId = "some_channel_id"; CharSequence channelName = "Some Channel"; int importance = NotificationManager.IMPORTANCE_LOW; NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance); notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.RED); notificationChannel.enableVibration(true); notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); notificationManager.createNotificationChannel(notificationChannel);
创建通知并设置频道
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); int notifyId = 1; String channelId = "some_channel_id"; Notification notification = new Notification.Builder(MainActivity.this) .setContentTitle("Some Message") .setContentText("You've received new messages!") .setSmallIcon(R.drawable.ic_notification) .setChannel(channelId) .build(); notificationManager.notify(id, notification);
此外,有关通知通道的详细说明,请遵循https://medium.com/exploring-android/exploring-android-o-notification-channels-94cd274f604c
推荐阅读
- python - Python Pandas Cumprod 问题
- sql - 尝试在 SQL Server 中创建规则时遇到问题
- java - “无法解析导入组织”
- ios - 如果我在 nib 中设置文本颜色并在资产中定义该颜色,UILabel 文本颜色不会以编程方式更改
- php - Compare meta_value in WP_Query that is stored in the form of serialized array
- python - 使用 docx.Document() 时的回溯(最后一次调用)
- docker - I cannot access proxy of a running docker container
- php - Codeigniter 图像上传到服务器文件夹
- php - Parse error: syntax error, unexpected 'connected' (T_STRING) in C:\xampp\htdocs\koneksi\update.php on line 5
- python - python3 django cant load imgur images to html if havent been open already manually