我想设置从服务器接收到的小图标通知。我的应用程序下载了一个图标,我想用这个图标显示通知。
我看到了这个问题,但找不到答案。
File myFile = new File(myPicAddress);
Notification noti = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentTitle("UPDATE APP")
.setContentText(myNotificationText)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.ic_launcher)//set myFile??
.setTicker("force android update!")
.setWhen(triggerTime)
.build();
我的应用已连接到服务器并下载我的图标,然后我想显示带有从服务器下载的图标的通知。
最佳答案
首先从url下载位图,然后将其设置为图标,如下所示
Bitmap bitmap = getBitmapFromURL("Your URL");
public Bitmap getBitmapFromURL(String strURL) {
try {
URL url = new URL(strURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
然后将位图设置为小图标
Notification noti = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentTitle("UPDATE APP")
.setContentText(myNotificationText)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(bitmap)//set myFile??
.setTicker("force android update!")
.setWhen(triggerTime)
.build();
已针对 API 23 及更高版本进行更新,如下所示
notificationBuilder.setSmallIcon(Icon.createWithBitmap(yourDownloadedBitmap));
关于android - 如何动态设置小图标通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47495052/