Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.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 新Firebase云消息传递系统的通知图标_Android_Push Notification_Firebase_Googleio_Firebase Cloud Messaging - Fatal编程技术网

Android 新Firebase云消息传递系统的通知图标

Android 新Firebase云消息传递系统的通知图标,android,push-notification,firebase,googleio,firebase-cloud-messaging,Android,Push Notification,Firebase,Googleio,Firebase Cloud Messaging,昨天,谷歌在谷歌I/O大会上展示了基于新Firebase的新通知系统。我在Github上尝试了这个新的FCM(Firebase云消息传递) 通知图标始终是ic_启动器,尽管我已经声明了一个特定的可绘制图标 为什么?? 下面是处理该消息的官方代码 public class AppFirebaseMessagingService extends FirebaseMessagingService { /** * Called when message is received.

昨天,谷歌在谷歌I/O大会上展示了基于新Firebase的新通知系统。我在Github上尝试了这个新的FCM(Firebase云消息传递)

通知图标始终是ic_启动器,尽管我已经声明了一个特定的可绘制图标

为什么?? 下面是处理该消息的官方代码

public class AppFirebaseMessagingService extends FirebaseMessagingService {

    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */
    // [START receive_message]
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
        sendNotification(remoteMessage);
    }
    // [END receive_message]

    /**
     * Create and show a simple notification containing the received FCM message.
     *
     * @param remoteMessage FCM RemoteMessage received.
     */
    private void sendNotification(RemoteMessage remoteMessage) {

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

// this is a my insertion looking for a solution
        int icon = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.drawable.myicon: R.mipmap.myicon;
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(icon)
                .setContentTitle(remoteMessage.getFrom())
                .setContentText(remoteMessage.getNotification().getBody())
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

}

不幸的是,这是SDK 9.0.0-9.6.1中Firebase通知的一个限制。当应用程序位于后台时,启动程序图标将从清单(带有必要的Android着色)中用于从控制台发送的消息

但是,使用SDK 9.8.0,您可以覆盖默认值!在AndroidManifest.xml中,可以设置以下字段以自定义图标和颜色:

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


请注意,如果应用程序位于前台(或发送数据消息),您可以完全使用自己的逻辑来定制显示。如果从HTTP/XMPP API发送消息,也可以始终自定义图标

我正在从FCM控制台并通过HTTP/JSON触发通知。。。同样的结果

我可以处理标题、完整消息,但图标始终是默认的白色圆圈:

而不是代码中的自定义图标(setSmallIcon或setSmallIcon)或应用程序中的默认图标:

 Intent intent = new Intent(this, MainActivity.class);
    // use System.currentTimeMillis() to have a unique ID for the pending intent
    PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

    if (Build.VERSION.SDK_INT < 16) {
        Notification n  = new Notification.Builder(this)
                .setContentTitle(messageTitle)
                .setContentText(messageBody)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pIntent)
                .setAutoCancel(true).getNotification();
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //notificationManager.notify(0, n);
        notificationManager.notify(id, n);
    } else {
        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

        Notification n  = new Notification.Builder(this)
                .setContentTitle(messageTitle)
                .setContentText(messageBody)
                .setSmallIcon(R.drawable.ic_stat_ic_notification)
                .setLargeIcon(bm)
                .setContentIntent(pIntent)
                .setAutoCancel(true).build();

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //notificationManager.notify(0, n);
        notificationManager.notify(id, n);
    }
Intent Intent=新的Intent(这是MainActivity.class);
//使用System.currentTimeMillis()为挂起的意图提供唯一的ID
PendingEvent pIntent=PendingEvent.getActivity(this,(int)System.currentTimeMillis(),intent,0);
如果(Build.VERSION.SDK_INT<16){
通知n=新建通知.Builder(此)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setSmallIcon(R.mipmap.ic_启动器)
.setContentIntent(pIntent)
.setAutoCancel(true).getNotification();
通知经理通知经理=
(通知经理)getSystemService(通知服务);
//notificationManager.notify(0,n);
notificationManager.notify(id,n);
}否则{
位图bm=BitmapFactory.decodeResource(getResources(),R.mipmap.ic_启动器);
通知n=新建通知.Builder(此)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setSmallIcon(R.drawable.ic_stat_ic_通知)
.setLargeIcon(bm)
.setContentIntent(pIntent)
.setAutoCancel(true).build();
通知经理通知经理=
(通知经理)getSystemService(通知服务);
//notificationManager.notify(0,n);
notificationManager.notify(id,n);
}

他们正在研究这个问题

当您从Firebase控制台发送通知时,默认情况下会使用您的应用程序图标,而Android系统在通知栏中会将该图标变为纯白色

如果您对该结果不满意,则应实现FirebaseMessagingService,并在收到消息时手动创建通知。我们正在努力改进这一点,但目前这是唯一的办法

编辑:使用SDK 9.8.0添加到AndroidManifest.xml

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

使用服务器实现向您的客户端发送消息,并使用数据类型的消息,而不是通知类型的消息


这将帮助您获得对
onMessageReceived
的回调,无论您的应用程序是在后台还是前台,您都可以生成自定义通知,然后只需将targetSdkVersion设置为19即可。通知图标将被着色。
然后等待Firebase解决此问题。

还有一种丑陋但有效的方法。反编译FirebaseMessagingService.class并修改其行为。然后,只需将该类放到yout应用程序中的正确包中,dex就可以使用它而不是消息库本身中的类。这是相当容易和工作

有一种方法:

private void zzo(Intent intent) {
    Bundle bundle = intent.getExtras();
    bundle.remove("android.support.content.wakelockid");
    if (zza.zzac(bundle)) {  // true if msg is notification sent from FirebaseConsole
        if (!zza.zzdc((Context)this)) { // true if app is on foreground
            zza.zzer((Context)this).zzas(bundle); // create notification
            return;
        }
        // parse notification data to allow use it in onMessageReceived whe app is on foreground
        if (FirebaseMessagingService.zzav(bundle)) {
            zzb.zzo((Context)this, intent);
        }
    }
    this.onMessageReceived(new RemoteMessage(bundle));
}

此代码来自版本9.4.0,由于混淆,方法在不同版本中将具有不同的名称

我的解决方案与ATom的类似,但更易于实现。您不需要创建一个完全隐藏FirebaseMessagingService的类,您只需重写接收意图的方法(至少在9.6.1版中是公共的),并从extras获取要显示的信息。“骇客”部分是,方法名称确实是模糊的,并且每次将Firebase sdk更新到新版本时都会更改,但是您可以通过使用Android Studio检查FirebaseMessagingService并寻找一个将意图作为唯一参数的公共方法来快速查找它。在9.6.1版中,它被称为zzm。 我的服务是这样的:

public class MyNotificationService extends FirebaseMessagingService {

    public void onMessageReceived(RemoteMessage remoteMessage) {
        // do nothing
    }

    @Override
    public void zzm(Intent intent) {
        Intent launchIntent = new Intent(this, SplashScreenActivity.class);
        launchIntent.setAction(Intent.ACTION_MAIN);
        launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* R    equest code */, launchIntent,
                PendingIntent.FLAG_ONE_SHOT);
        Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(),
                R.mipmap.ic_launcher);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_notification)
                .setLargeIcon(rawBitmap)
                .setContentTitle(intent.getStringExtra("gcm.notification.title"))
                .setContentText(intent.getStringExtra("gcm.notification.body"))
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager)     getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

如果您的应用程序位于后台,则通知图标将设置为Message Receive method,但如果您的应用程序位于前台,则通知图标将是您在清单上定义的图标

写下这个

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


我想我会给这个问题添加一个答案,因为我的问题很简单,但很难注意到。尤其是在创建我的
com.google.firebase.messaging.default_notification_图标时,我复制/粘贴了一个现有的元数据元素,该图标使用
android:value
标记来指定其值。这对通知图标不起作用,一旦我将其更改为
android:resource
,一切正常。

firebase与您创建通知的方式无关,请提供一张您看到的确切图像。此代码直接来自Firebase,sendNotification()方法对于任何通知都完全相同。这段代码在GCM上运行良好,但在FCM号上运行良好。它始终保持ic_启动器状态,使用新的web