java - 如何在android中自定义通知

标签 java android firebase push-notification google-cloud-firestore

我刚刚使用主题消息传递实现了 firebase 云消息传递 pushNotification。收到通知,但 MyFirebaseMessagingService 未正常工作。它传递的消息与云消息一样。

完成云消息功能。 创建服务名称 MyFirebaseMessagingService 并使用 FirebaseMessagingService 进行扩展。 将服务添加到 list 。

这是我的 FirebaseMessagingService 类。

@Override
public void onDeletedMessages() {
    super.onDeletedMessages();
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    String messageTitle = remoteMessage.getNotification().getTitle();
    String clickAction = remoteMessage.getNotification().getClickAction();
    Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+ getApplicationContext().getPackageName() + "/" + R.raw.hollow);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setContentTitle("New Notification")
            .setSound(soundUri)
            .setContentText(messageTitle);

    Intent resultIntent = new Intent(clickAction);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    builder.setContentIntent(pendingIntent);
    builder.setPriority(Notification.PRIORITY_HIGH);
    if (Build.VERSION.SDK_INT >= 21) builder.setVibrate(new long[0]);

    int mNotificationId = (int) System.currentTimeMillis();

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    notificationManager.notify(mNotificationId, builder.build());

}

我的 list 文件:-

<service
            android:name=".MyFirebaseMessagingService"
            android:exported="true">
            <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/default_notification_channel_id" />

最佳答案

在发送云消息的同时,您还必须订阅要接收通知的主题,例如。

FirebaseMessaging.getInstance().subscribeToTopic("general")
                .addOnCompleteListener(task -> {
                    if (task.isSuccessful())
                        Toast.makeText(this, "Subs-Successful", Toast.LENGTH_SHORT).show();
                    else
                        Toast.makeText(this, "Subs NOT Successful", Toast.LENGTH_SHORT).show();
                });

在此我订阅了一般主题,并且只会收到关于这个主题的消息,就像这个一样。

See this pic

希望这对我有帮助......对我有用!

关于java - 如何在android中自定义通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57685393/

相关文章:

java - CloudBees 的 JMS 代理选项?

android - 使用 getValue() 从 DataSnapshot 解析到 Firebase 中的 Java 类

java - 为4.0蓝牙传输创建一个socket

javascript - 在 React Native 中将 base64 字符串转换为图像

swift - 寻找一种更好的方法来区分 2 个数组

ios - 如何将数据存储到 Firestore 中具有相同 ID 的多个集合?

java - 使用随机整数询问乘法表

java - Thread 类如何在另一个类中调用?

c# - 面向 C# 程序员的高级 Java 主题

android - 无法使用 Travis-CI 和 Android 构建项目