OneSignal-Pushnotification small icon not displaye

2019-05-11 03:07发布

问题:

We have a Cordova-App which contains a file 'icon.png' in every drawable-xy folder. We provide push notifications using OneSignal. The notifications are working as expected. Except the small icon. There is only a blank icon on the top bar of my phone, where app-symbols are displayed. I tried using the following snippet:

{
   ...
   "small_icon" : "@android:drawable/icon.png"
   ....
}

I also tried lots and lots of other variations without "android", without "drawable", without slash, with .png and without .png. etc. etc.

I found this in the documentation: (https://documentation.onesignal.com/reference#create-notification)

small_icon: stringOptional
Specific Android icon to use. If blank the app icon is used. Must be the drawable resource name.

Android tells about drawable resources: (https://developer.android.com/guide/topics/resources/drawable-resource.html)

With an image saved at res/drawable/myimage.png, this layout XML applies the image to a View:

<ImageView
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:src="@drawable/myimage" />

which should be @drawable/icon for me then. (does not work)

Changing the large icon to an url works for me, large icon with a drawable does not, aswell.

What is the correct usage of this property small_icon?

回答1:

After Android SDK API 21,your small icon must be like this

white and transparent only.

So, you can code this.

if(android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.LOLLIPOP){
        notificationBuilder.setSmallIcon(R.drawable.ic_aphla_logo);
    } else {
        notificationBuilder.setSmallIcon(R.drawable.ic_logo);
    }


回答2:

The small icon is a Android resource which is a name without a file extension or path. In your case you need to set "small_icon" : "icon" in the JSON payload sent to the OneSignal create notification REST API call.

For Cordova you need to place your icon files in <project-root>/platforms/android/res. There should be one for each resolution and using the Android Asset Studio the recommend way to create this small icon. If you set the name to ic_stat_onesignal_default it will be used by the OneSignal SDK for all OneSignal notifications unless you override it in the JSON message. The full path for one of the icon files should be <project-root>/platforms/android/res/drawable-hdpi-v11/ic_stat_onesignal_default.png for example.

See OneSignal's Customize Notification Icons guide for full details.