java - android 中推送通知到达后如何更改家庭 Activity 中的通知图标

标签 java android kotlin

我在我的应用中使用推送通知,我需要在通知到达后立即更改我的家庭 Activity 中的通知图标。我们可以使用 firebase 服务内部的界面来实现此目的吗?

最佳答案

您可以使用自定义接收器 firebase Clude 消息

在你的 list 中

      <service
        android:name=".ReciverClass.MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

和你的接收者

public class MyFirebaseMessagingService extends FirebaseMessagingService {
/**
 * Called when message is received.
 *
 * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
 */
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    // TODO(developer): Handle FCM messages here.
    // If the application is in the foreground handle both data and notification messages here.
    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
    RemoteMessage.Notification notification = remoteMessage.getNotification();
    Map<String, String> data = remoteMessage.getData();

    sendNotification(notification, data);
}

private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {
    Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")
            .setContentTitle(notification.getTitle())
            .setContentText(notification.getBody())
            .setAutoCancel(true)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setContentIntent(pendingIntent)
            .setContentInfo(notification.getTitle())
            .setLargeIcon(icon)
            .setColor(Color.RED)
            .setLights(Color.RED, 1000, 300)
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setSmallIcon(R.mipmap.ic_launcher);

    try {
        String picture_url = data.get("picture_url");
        if (picture_url != null && !"".equals(picture_url)) {
            URL url = new URL(picture_url);
            Bitmap bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            notificationBuilder.setStyle(
                    new NotificationCompat.BigPictureStyle().bigPicture(bigPicture).setSummaryText(notification.getBody())
            );
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Notification Channel is required for Android O and above
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(
                "channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT
        );
        channel.setDescription("channel description");
        channel.setShowBadge(true);
        channel.canShowBadge();
        channel.enableLights(true);
        channel.setLightColor(Color.RED);
        channel.enableVibration(true);
        channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500});
        notificationManager.createNotificationChannel(channel);
    }

    notificationManager.notify(0, notificationBuilder.build());
}}

祝你好运

关于java - android 中推送通知到达后如何更改家庭 Activity 中的通知图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58056021/

相关文章:

java - XJC 与 Maven 的片段

android - 如何在 android 2.1 中启动 QuickContact 操作

android - 如何将xml数据存入sqlite数据库

kotlin - 如何执行伴随对象 lateinit 是初始化检查

java - PHP 在 sql 查询中使用 $_GET 不返回结果

java - 如何使用 java 验证 Selenium 中的字符串列表是否按字母顺序排列

java - 为什么在 java 中解码和编码字符串并不总是对称的?

javascript - 如何让这些组件在 react 中导航到另一个屏幕?

kotlin - Kotlin中的非空检查有什么区别?

android - 由于 ModelClass 在 KotlinMultiplatform 应用程序中不是可序列化或可解析的,导致 nav_graph 参数错误