o-town
o-town

Reputation: 143

Firebase Cloud Messaging background notification channel ID not being set in Android O

Firebase doesn't seem to be setting the Notification channelId on incoming messages to android M. I'm following this guide, and trying to get notifications to fire when my app is in the background.

Here's my app code and manifest.

public class MainActivity extends AppCompatActivity {

    private void registerNotificationChannel(String id, String name, String description) {
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(id, name, importance);
        mChannel.setDescription(description);
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.RED);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        mNotificationManager.createNotificationChannel(mChannel);
    }

    //private BroadcastReceiver isms;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG,"MainActivity.oncreate. token is:" +  FirebaseInstanceId.getInstance().getToken());

        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        registerNotificationChannel("default","default","all other notifications");
        registerNotificationChannel("channel1","channel1","notification channel 1");
        registerNotificationChannel("channel2","channel2","notification channel 2");
    }

    @Override protected void onDestroy() {
        super.onDestroy();
    }

}

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "firebase message received");
        if (remoteMessage.getNotification() != null) {
            Map<String,String> data = remoteMessage.getData();
            for (Map.Entry<String, String> entry : data.entrySet())
            {
                Log.d(TAG, "data: " + entry.getKey() + "/" + entry.getValue());
            }
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }

    }
}

    public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed firebase " +
                "" +
                "" +
                "token: " + refreshedToken);
    }
}

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.phauna.alerter">

    <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">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="default"/>

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

    </application>

</manifest>

As you can see, I'm definitely registering my notification channels as it says here. This screenshot from my device confirms the channels are registered as well:

enter image description here

Here's the critical part. When I target API version 25, I get background notifications. As soon as I target version 26, I do not. Instead, I get this error message in logcat:

10-11 12:40:00.925   899  8910 E NotificationService: No Channel found for pkg=org.phauna.alerter, channelId=null, id=0, tag=GCM-Notification:286245598, opPkg=org.phauna.alerter, callingUid=10179, userId=0, incomingUserId=0, notificationUid=10179, notification=Notification(channel=null pri=0 contentView=null vibrate=null sound=content://settings/system/notification_sound defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE)

I'm definitely setting a channel ID, as depicted in this screenshot sending from the Firebase console:

enter image description here

I have also tried sending through the HTTP service:

curl -X POST --header "Authorization: key=<redacted>" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"<redacted>\",\"data\":{\"android_channel_id\":\"channel1\"},\"notification\":{\"body\":\"this is a testfoobar\"}}"

In both cases, the message reaches my device but does not have a channelId (as indicated by channelId=null in the above log message).

In the foreground, I can catch the notification with the service and manually stick a channel ID on it (even if I have to encode the channel ID in the payload of the message). But I need the background notifications to work as well, and as far as I know, that's up to the Firebase library to get right.

Upvotes: 1

Views: 11641

Answers (1)

Bob Snyder
Bob Snyder

Reputation: 38299

The Firebase Release Notes indicate that support for notification channels was added in version 10.2.6 (May 17, 2017):

Added support for Android O notification channels. Android clients can specify a default notification channel in the application manifest which will be used if the downstream message does not contain a notification_channel parameter.

Update your build to use version 10.2.6 or later.

Upvotes: 3

Related Questions