Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: OneSignal how to change Notification bill icon with custom icon or app icon?

When notification is received the bill icon is shown instead of app icon or custom icon that i override it to be shown from code. i could to change the icon from the dashboard but i want to handle it from the code

Intilaization

    OneSignal.startInit(this)
            .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)// to hide dialog
            .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
            .init();

Receiver class

  class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler
{
    // This fires when a notification is opened by tapping on it.
    @Override
    public void notificationOpened(OSNotificationOpenResult result)
    {
        OSNotificationAction.ActionType actionType = result.action.type;
        JSONObject data = result.notification.payload.additionalData;
        String customKey;

        Intent intent = new Intent(Roshetta.app, SplashActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);


        int requestCode = 0;

            PendingIntent pendingIntent = PendingIntent.getActivity(Roshetta.app, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
            Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

            android.support.v4.app.NotificationCompat.Builder noBuilder = new android.support.v4.app.NotificationCompat.Builder(Roshetta.app)
                    .setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(largeIcon).setContentTitle(result.notification.payload.title)
                    .setContentText(result.notification.payload.body )
                    .setAutoCancel(true).setDefaults(android.app.Notification.DEFAULT_ALL)
                    .setContentIntent(pendingIntent).setSound(sound);


            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
             notificationManager.notify(0, noBuilder.build()); //0 = ID of notification


            if (data != null)
            {
                customKey = data.optString("customkey", null);
                if (customKey != null)
                    Log.i("OneSignalExample", "customkey set with value: " + customKey);
            }

            if (actionType == OSNotificationAction.ActionType.ActionTaken)
                Log.i("OneSignalExample", "Button pressed with id: " + result.action.actionID);


            Log.i("OneSignalExample", "ExampleNotificationOpenedHandler");

}

like image 222
Mina Farid Avatar asked Nov 17 '16 20:11

Mina Farid


People also ask

How do I change the icon on my OneSignal notification?

You need to create a icon named ic_stat_onesignal_default in your drawables directory which will be shown instead of OneSignal's default bell icon. It is recommend to use the Android Asset Studio to create the correct sizes and to make sure it will look correct before trying it on your device.

How do I change app notification icons on Android?

1 Tap Notification Settings on the notification panel or tap the Settings app. 2 Tap Notifications. 3 Tap App icon badges.


2 Answers

You need to create a icon named ic_stat_onesignal_default in your drawables directory which will be shown instead of OneSignal's default bell icon. It is recommend to use the Android Asset Studio to create the correct sizes and to make sure it will look correct before trying it on your device.

Note the app icon should no longer be used as a small notification icon as in Android 5.0 Lollipop the icon's alpha will only be used. Most app icons will just be a white squire or circle in this case.

See OneSignal's documentation page below for more details. https://documentation.onesignal.com/docs/customize-notification-icons

like image 51
jkasten Avatar answered Sep 28 '22 01:09

jkasten


You must create icons for the following sizes:

drawable-hdpi/ic_stat_one_signal_default.png

drawable-mdpi/ic_stat_one_signal_default.png

drawable-xhdpi/ic_stat_one_signal_default.png

drawable-xxhdpi/ic_stat_one_signal_default.png

drawable-xxxhdpi/ic_onesignal_large_icon_default.png

Note that the name for size drawable-xxxhdpi is different.

I recommend creating icons with Android Asset Studio or from Android Studio->app->Image Asset to be sure to have the icons for different android versions.

If you want to have two icons type, one for notification area and one for notification drawer you must do the following:

1.- Create the same image icon for different sizes: mdpi, hdpi, xhdpi, xxhpi called: ic_stat_one_signal_default

2.- Create a different image icon for size xxxhdpi called: ic_onesignal_large_icon_default

Looks like: Icon - Notification area/drawer

See OneSignal's Oficial Documentation for more information. https://documentation.onesignal.com/docs/customize-notification-icons

like image 23
gquilarque Avatar answered Sep 28 '22 01:09

gquilarque