0

I send push notification to android phone. Almost phones are receive and show notification except one phone(Samsung Galaxy Note 5).

The phone shows notification icon and goes at the same time. You can see this issue followed url (refer 7sec and 22sec): https://youtu.be/J6UZm_6mqP0

Here is AndroidMenifest.xml of my app

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/ic_stat_notification" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="@string/default_notification_channel_id" />

    <activity
        android:name=".MainActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".FCMInstanceService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>
    <service
        android:name=".MyFirebaseMessagingService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
</application>

Where do i start to solve for this problem?

--More--

MyFirebaseMessagingService.java

package com.smartivt.smartivtmessenger;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.Log;

import com.google.firebase.messaging.RemoteMessage;

import java.util.List;

public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

    private static final String TAG = "FirebaseMessaging";

    private final String CHANNEL_ID = "DEFAULT_CHANNEL";
    private final String CHANNEL_NAME = "Default Channel";
    private final String GROUP_NAME = "MESSAGE_GROUP";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        NotificationManager notiMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if ( remoteMessage.getData().get("title") != null ) {
            Log.d(TAG, "onMessageReceived: " + remoteMessage.getData().get("title"));
            Log.d(TAG, "onMessageReceived: " + remoteMessage.getData().get("body"));

            if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) {
                Notification.Builder notify = new Notification.Builder(getApplicationContext(), CHANNEL_ID);

                notify.setSmallIcon(R.drawable.ic_stat_notification);
                notify.setContentTitle(remoteMessage.getData().get("title"));
                notify.setContentText(remoteMessage.getData().get("body"));
                notify.setAutoCancel(true);

                notiMgr.notify(NotificationID.getID(), notify.build());

                /*
                Intent badgeIntent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");

                badgeIntent.putExtra("badge_count", 5);
                badgeIntent.putExtra("badge_count_package_name", getPackageName());
                badgeIntent.putExtra("badge_count_class_name", getLauncherClassName());
                sendBroadcast(badgeIntent);
                */
                Log.d(TAG, "update badge: " + getPackageName() + ", " + getLauncherClassName());
            }
            else {
                Notification.Builder notify = new Notification.Builder(getApplicationContext());

                notify.setSmallIcon(R.drawable.ic_stat_notification);
                notify.setContentTitle(remoteMessage.getData().get("title"));
                notify.setContentText(remoteMessage.getData().get("body"));
                notify.setAutoCancel(true);

                int id = NotificationID.getID();

                Log.d(TAG, "id: " + id);
                notiMgr.notify(id, notify.build());
            }
        }
        else if ( remoteMessage.getNotification() != null ) {
            Log.d(TAG, "onMessageReceived2: " + remoteMessage.getNotification().getTitle());
            Log.d(TAG, "onMessageReceived2: " + remoteMessage.getNotification().getBody());

            if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) {
                Notification.Builder notify = new Notification.Builder(getApplicationContext(), CHANNEL_ID);

                notify.setSmallIcon(R.drawable.ic_stat_notification);
                notify.setContentTitle(remoteMessage.getNotification().getTitle());
                notify.setContentText(remoteMessage.getNotification().getBody());
                notify.setAutoCancel(true);

                notiMgr.notify(NotificationID.getID(), notify.build());
            }
            else {
                Notification.Builder notify = new Notification.Builder(getApplicationContext());

                notify.setSmallIcon(R.drawable.ic_stat_notification);
                notify.setContentTitle(remoteMessage.getNotification().getTitle());
                notify.setContentText(remoteMessage.getNotification().getBody());
                notify.setAutoCancel(true);

                notiMgr.notify(NotificationID.getID(), notify.build());
            }
        }
    }

    @Nullable
    private String getLauncherClassName () {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        PackageManager pm = getApplicationContext().getPackageManager();
        List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
        for (ResolveInfo resolveInfo : resolveInfos) {
            String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
            if ( pkgName.equalsIgnoreCase(getPackageName())) {
                return resolveInfo.activityInfo.name;
            }
        }

        return null;
    }
}
Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41
재구김
  • 1
  • 1

1 Answers1

1

You don't need two services now..... Check this topic:

FirebaseInstanceIdService is deprecated now.

You dont need this.

<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />

Use it like this.

AndroidManifest.xml

 <service
        android:name=".MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
 </service>

MyFirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    public static int NOTIFICATION_ID = 1;

    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
      //Code to create the Notification

    }
}

gradle build

implementation 'com.google.firebase:firebase-core:16.0.3'
implementation 'com.google.firebase:firebase-messaging:17.3.0'
Yasiru Nayanajith
  • 1,647
  • 17
  • 20