Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何更改收到通知的方形图标?_Android_Firebase Cloud Messaging - Fatal编程技术网

Android 如何更改收到通知的方形图标?

Android 如何更改收到通知的方形图标?,android,firebase-cloud-messaging,Android,Firebase Cloud Messaging,我正在我的应用程序中设置Firebase云消息,除了通知图标外,其他一切都正常工作。每当我收到通知时,通知图标就会显示为白色正方形 我已经尝试将builder.setSmallIcon更改为其他图标,并且在AndroidManifest.xml中添加了firebase消息传递默认通知的资源图标 FcmMessagingService.java private static int VIBRATION_TIME = 500; // in millisecond private SharedPref

我正在我的应用程序中设置Firebase云消息,除了通知图标外,其他一切都正常工作。每当我收到通知时,通知图标就会显示为白色正方形

我已经尝试将builder.setSmallIcon更改为其他图标,并且在AndroidManifest.xml中添加了firebase消息传递默认通知的资源图标

FcmMessagingService.java

private static int VIBRATION_TIME = 500; // in millisecond
private SharedPref sharedPref;

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    sharedPref = new SharedPref(this);
    if (sharedPref.getNotification()) {
        // play vibration
        if (sharedPref.getVibration()) {
            ((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(VIBRATION_TIME);
        }
        RingtoneManager.getRingtone(this, Uri.parse(sharedPref.getRingtone())).play();

        if (remoteMessage.getData().size() > 0) {
            Map<String, String> data = remoteMessage.getData();
            FcmNotif fcmNotif = new FcmNotif();
            fcmNotif.setTitle(data.get("title"));
            fcmNotif.setContent(data.get("content"));
            fcmNotif.setPost_id(Integer.parseInt(data.get("post_id")));
            displayNotificationIntent(fcmNotif);
        }
    }
}

private void displayNotificationIntent(FcmNotif fcmNotif) {
    Intent intent = new Intent(this, ActivitySplash.class);
    if (fcmNotif.getPost_id() != -1) {
        intent = new Intent(this, ActivityPostDetails.class);
        Post post = new Post();
        post.title = fcmNotif.getTitle();
        post.id = fcmNotif.getPost_id();
        boolean from_notif = !ActivityMain.active;
        intent.putExtra(ActivityPostDetails.EXTRA_OBJC, post);
        intent.putExtra(ActivityPostDetails.EXTRA_NOTIF, from_notif);
    }
    PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

    Notification.Builder builder = new Notification.Builder(this);
    builder.setContentTitle(fcmNotif.getTitle());
    builder.setSmallIcon(R.drawable.ic_notification2);
    //builder.setSmallIcon(R.drawable.splash_icon);
    //builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),
         R.mipmap.ic_launcher));
    builder.setStyle(new Notification.BigTextStyle().bigText(fcmNotif.getContent()));
    builder.setContentText(fcmNotif.getContent());
    builder.setDefaults(Notification.DEFAULT_LIGHTS);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        builder.setPriority(Notification.PRIORITY_HIGH);
    }
    builder.setContentIntent(pendingIntent);
    builder.setAutoCancel(true);
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    int unique_id = (int) System.currentTimeMillis();
    notificationManager.notify(unique_id, builder.build());
}
private static int-VIBRATION\u TIME=500;//毫秒
私人股ref SharedPref;
@凌驾
收到消息时公共无效(RemoteMessage RemoteMessage){
sharedPref=新的sharedPref(此);
if(sharedPref.getNotification()){
//播放振动
if(sharedPref.getVibration()){
((可控震源)getSystemService(Context.可控震源服务)).VIBRATION(振动时间);
}
getRingtone(这个,Uri.parse(sharedPref.getRingtone()).play();
如果(remoteMessage.getData().size()>0){
Map data=remoteMessage.getData();
FcmNotif FcmNotif=新的FcmNotif();
fcmNotif.setTitle(data.get(“title”);
fcmNotif.setContent(data.get(“content”));
fcmNotif.setPost_id(Integer.parseInt(data.get(“post_id”)));
显示通知意图(fcmNotif);
}
}
}
私有无效显示通知意图(FcmNotif FcmNotif){
Intent Intent=新的Intent(这是ActivitySplash.class);
如果(fcmNotif.getPost_id()!=-1){
intent=新的intent(这是ActivityPostDetails.class);
Post Post=新Post();
post.title=fcmNotif.getTitle();
post.id=fcmNotif.getPost_id();
来自_notif的布尔值=!ActivityMain.active;
intent.putExtra(ActivityPostDetails.EXTRA_OBJC,post);
intent.putExtra(ActivityPostDetails.EXTRA\u NOTIF,from\u NOTIF);
}
PendingEvent PendingEvent=PendingEvent.getActivity(this,(int)System.currentTimeMillis(),intent,0);
Notification.Builder=new Notification.Builder(此);
setContentTitle(fcmNotif.getTitle());
建筑商设置尺寸图标(R.drawable.ic_notification2);
//builder.setSmallIcon(R.drawable.splash_图标);
//builder.setSmallIcon(R.mipmap.ic_启动器);
builder.setLargeIcon(BitmapFactory.decodeResource)(this.getResources(),
R.mipmap.ic_启动器);
builder.setStyle(new Notification.BigTextStyle().bigText(fcmNotif.getContent());
setContentText(fcmNotif.getContent());
builder.setDefaults(Notification.DEFAULT_LIGHTS);
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.JELLY\u BEAN){
builder.setPriority(通知优先级高);
}
builder.setContentIntent(挂起内容);
builder.setAutoCancel(true);
NotificationManager NotificationManager=(NotificationManager)getSystemService(通知服务);
int unique_id=(int)System.currentTimeMillis();
notificationManager.notify(唯一的_id,builder.build());
}

您需要调用
builder.setSmallIcon(getNotificationIcon())
并确保图标背景应该是透明的,即
R.mipmap.transparent
,如果您的
Build.VERSION.SDK\u INT
LOLLIPOP
或更高版本
Build.VERSION\u CODES.LOLLIPOP
,则可以使用您的应用程序图标

 private int getNotificationIcon()
 {
    boolean useWhiteIcon = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
    return useWhiteIcon ? R.mipmap.transparent: R.mipmap.appicon;
 }
Firebase 9.8.0可以通过在清单中添加有关此图标的信息来更改此图标:

有关推送通知,请参阅此链接,直到我们使用的5.0版本通知图标显示为止,但在5.0版本之后,您必须为推送通知设置透明通知图标。当你在那里设置图标时,你需要为版本检查应用条件,在链接中你会得到你想要的正确答案。并将通知图标保存在所有相应的文件夹中,如-hdpi、mdpi、xhdpi、xxhdpi、xxxhdpi。不要将通知图标放在drawable文件夹中。从以下链接生成通知图标-
 Firebase 9.8.0 it is possible to change this icon, by adding info about this in Manifest:

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