How to set a bigger icon in Firebase app notification?

这一生的挚爱 提交于 2019-12-05 23:14:51

问题


Hi i am implementing Push Notifications in Android using Firebase. Now, there is a small icon showing inside one circle. I need it to show in bigger size. Please see the image. And here is my code,

            android:id="@+id/relativeNotification"

            android:layout_width="192dp"
            android:layout_height="192dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true">

            <com.inspius.coreapp.widget.TintableImageView
                android:layout_width="192dp"
                android:layout_height="192dp"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_gravity="center_vertical"
                android:contentDescription="@string/app_name"
                android:src="@drawable/ic_launcher"
                app:tint="@color/custom_icon_video_detail_selector" />

How do i set large icon from this small icon?


回答1:


I think it can be done by overriding the default settings.

In your application manifest:

<meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/notification_icon" />

Use your icon instead of @drawable/notification_icon.

Source

Hope that helps

Update: Also, Have a look:

https://github.com/firebase/quickstart-android/issues/4#issuecomment-221344318

The icon parameter can be used to specify a drawable within your app. If you want to use R.drawable.foo, just pass foo.

Here is the icon parameter documentation:

https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support




回答2:


See You can customize your firebase notification only when your app is in foreground.

Your notification will be caught in onMessageReceived method of FirebaseMessagingService service

To Customize your notification when your app is in foreground.

Put Icon From App Drawable Folder

Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.icons);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setLargeIcon(largeIcon)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody());
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            manager.notify(0, builder.build());

Put Icon From API "icon" tag

String name = remoteMessage.getNotification().getIcon();
URL url_value = new URL(name);
Bitmap mIcon1 = BitmapFactory.decodeStream(url_value.openConnection().getInputStream());
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setLargeIcon(mIcon1)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(remoteMessage.getNotification().getTitle())            .setContentText(remoteMessage.getNotification().getBody());
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            manager.notify(0, builder.build());



回答3:


I tried to do the same thing. Overriding "handleIntent" method worked for me. I guess removing the super.handleIntent(intent) made this work. Then we can build our own notification to set the large icon. Both foreground and background cases would call this method. Something like this:

public class YourMessagingService extends FirebaseMessagingService {

    @Override
    public void handleIntent(Intent intent) {
        // super.handleIntent(intent);

        // get intent codes....

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        Notification notification = builder
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.yourLargeIcon))
                .setSmallIcon(R.drawable.yourSmallIcon)
                .setContentTitle("yourTitle")
                .setContentText("yourText")
                .build();

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(111, notification);
    }

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

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


来源:https://stackoverflow.com/questions/41479579/how-to-set-a-bigger-icon-in-firebase-app-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!