태그 보관물: firebase-cloud-messaging

firebase-cloud-messaging

새로운 Firebase Cloud Messaging 시스템이 포함 된 알림 아이콘 receive_message] @Override

어제 Google은 Google I / O에서 새로운 Firebase를 기반으로하는 새로운 알림 시스템을 발표했습니다. 이 새로운 FCM (Firebase Cloud Messaging)을 Github의 예제와 함께 사용해 보았습니다.

알림의 아이콘은 특정 드로어 블을 선언했지만 항상 ic_launcher입니다.

왜 ? 메시지 처리를위한 공식 코드는 다음과 같습니다.

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에서 메시지를 보내는 경우 항상 아이콘을 사용자 정의 할 수 있습니다.


답변

서버 구현을 사용하여 클라이언트에 메시지를 보내고 알림 유형의 메시지가 아닌 데이터 유형의 메시지를 사용하십시오 .

onMessageReceived앱이 백그라운드 또는 포 그라운드에 있고 사용자 정의 알림을 생성 할 수 있는지 여부 에 관계없이 콜백을 얻는 데 도움이됩니다.


답변

atm 그들은 그 문제에 대해 작업하고 있습니다 https://github.com/firebase/quickstart-android/issues/4

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"/>


답변

내 솔루션은 ATom의 솔루션과 유사하지만 구현하기가 더 쉽습니다. FirebaseMessagingService를 완전히 가리는 클래스를 만들 필요는 없습니다. 인 텐트 (적어도 버전 9.6.1에서는 공개)를 수신하는 메소드를 재정의하고 추가 정보에서 표시 할 정보를 가져올 수 있습니다. “해킹”부분은 메소드 이름이 실제로 난독 화되어 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());
    }
}


답변

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의 코드이며, 난독 화로 인해 다른 버전의 이름이 다른 메소드가 있습니다.


답변

앱이 백그라운드에있는 경우 알림 아이콘은 Message Receive 메소드에 설정되지만 앱이 포 그라운드에있는 경우 알림 아이콘은 매니페스트에 정의한 것입니다.

여기에 이미지 설명을 입력하십시오