Skip to content

Commit

Permalink
Add support for Android notification channels.
Browse files Browse the repository at this point in the history
Change-Id: Id5773a3cd6783ed070db4898077560ec4d312efa
  • Loading branch information
kroikie committed May 5, 2017
1 parent 974a9df commit 4c85680
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
16 changes: 16 additions & 0 deletions messaging/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 51,22 @@ Use Firebase console to send FCM messages to device or emulator.
Best Practices
--------------

## Android notification channels

### Set default channel

If incoming FCM messages do not specify an Android notification channel, you can indicate
to FCM what channel should be used as the default by adding a metadata element to your
application manifest. In the metadata element specify the ID of the channel that should
be used by default by FCM.

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

Note: You are still required to create a notification channel in code with an ID that
matches the one defined in the manifest. See the Android [docs](https://goo.gl/x9fh5X) for more.

## Customize default notification

### Custom default icon
Expand Down
4 changes: 2 additions & 2 deletions messaging/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
compileSdkVersion 'android-O'
buildToolsVersion '25.0.3'

defaultConfig {
applicationId "com.google.firebase.quickstart.fcm"
minSdkVersion 14
targetSdkVersion 25
targetSdkVersion 'O'
versionCode 1
versionName "1.0"

Expand Down
5 changes: 5 additions & 0 deletions messaging/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 19,11 @@
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
<!-- [END fcm_default_icon] -->
<!-- [START fcm_default_channel] -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel"
android:value="@string/default_notification_channel_id"/>
<!-- [END fcm_default_channel] -->
<activity
android:name="com.google.firebase.quickstart.fcm.MainActivity"
android:label="@string/app_name">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 16,10 @@

package com.google.firebase.quickstart.fcm;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
Expand All @@ -35,6 39,16 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create channel to show notifications.
String channelId = getString(R.string.default_notification_channel_id);
String channelName = getString(R.string.default_notification_channel_name);
NotificationManager notificationManager =
getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_LOW));
}

// If a notification message is tapped, any data accompanying the notification
// message is available in the intent extras. In this sample the launcher
// intent is fired when the notification is tapped, so any accompanying data would
Expand Down
7 changes: 7 additions & 0 deletions messaging/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 8,11 @@

<string name="msg_subscribed">Subscribed to news topic</string>
<string name="msg_token_fmt" translatable="false">InstanceID Token: %s</string>

<string name="default_notification_channel_id" translatable="false">fcm_default_channel</string>
<!--
This is the name that users will see when interacting with this channel.
It should describe the category of notifications that will be sent through this channel
-->
<string name="default_notification_channel_name" translatable="true">News</string>
</resources>

0 comments on commit 4c85680

Please sign in to comment.