AndroidO中如何设置Firebase通知ChannelID?
How set Firebase notification ChannelID in Android O?
对于API 26级,我们必须设置一个频道ID作为参考。我学会了如何在没有 channelID 的情况下执行此操作,下面是我的 firebase 消息传递设置代码。但是现在新的Androidapi26级
NotificationCompat.Builder(this);
这不起作用,我应该添加对 channelID 的引用作为
NotificationCompat.Builder(this, channelID);
但是我不知道该怎么做。
你能帮助我了解如何为 Firebase 云消息传递创建通道 ID 吗?
AndroidManifest.xml
<service android:name=".MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"></action>
</intent-filter>
</service>
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"></action>
</intent-filter>
</service>
MyFirebaseInstanceIdService Java Class:
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
private static final String REG_TOKEN = "REG_TOKEN";
@Override
public void onTokenRefresh() {
String recent_token = FirebaseInstanceId.getInstance().getToken();
Log.d(REG_TOKEN, recent_token);
}
}
MyFirebaseMessagingService Java Class
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
// Here I will set up a channel ID as "CH_ID_1" but I do not know how to do this.
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "CH_ID_1");
//****************************************************
notificationBuilder.setContentTitle("FCM NOTIFICATION");
notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
Gradle - 依赖关系
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.android.support:design:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-core:16.0.0'
implementation 'com.google.firebase:firebase-messaging:17.0.0'
}
apply plugin: 'com.google.gms.google-services'
我之前在一个应用中做的是应用一启动就初始化Notification Channels。所以我在我的应用程序 class 中添加了一个 init
函数,如下所示:
@TargetApi(Build.VERSION_CODES.O)
private void initNotificationChannels() {
NotificationChannel publicChannel = new NotificationChannel(
Constants.NOTIFICATION_CHANNEL_PUBLIC,
Constants.NOTIFICATION_CHANNEL_PUBLIC,
NotificationManager.IMPORTANCE_DEFAULT);
publicChannel.setDescription(Constants.NOTIFICATION_CHANNEL_PUBLIC);
NotificationChannel topicChannel = new NotificationChannel(
Constants.NOTIFICATION_CHANNEL_TOPIC,
Constants.NOTIFICATION_CHANNEL_TOPIC,
NotificationManager.IMPORTANCE_DEFAULT);
topicChannel.setDescription(Constants.NOTIFICATION_CHANNEL_TOPIC);
NotificationChannel privateChannel = new NotificationChannel(
Constants.NOTIFICATION_CHANNEL_PRIVATE,
Constants.NOTIFICATION_CHANNEL_PRIVATE,
NotificationManager.IMPORTANCE_HIGH);
privateChannel.setDescription(Constants.NOTIFICATION_CHANNEL_PRIVATE);
privateChannel.canShowBadge();
List<NotificationChannel> notificationChannels = new ArrayList<>();
notificationChannels.add(publicChannel);
notificationChannels.add(topicChannel);
notificationChannels.add(privateChannel);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannels(notificationChannels);
}
}
然后在我的 FirebaseMessagingService
中,我创建了一个在构建通知时获取 channelId
的函数:
private String getChannelId(String source) {
if (!TextUtils.isEmpty(source)) {
if (source.contains(Constants.TOPIC_PREFIX)) {
return (TextUtils.equals((TOPIC_PREFIX + Constants.NOTIFICATION_CHANNEL_PUBLIC), source)) ?
Constants.NOTIFICATION_CHANNEL_PUBLIC : Constants.NOTIFICATION_CHANNEL_TOPIC;
} else {
return Constants.NOTIFICATION_CHANNEL_PRIVATE;
}
} else {
return Constants.NOTIFICATION_CHANNEL_PUBLIC;
}
}
这满足了我们的应用程序需要的三种类型的通知——Public、主题和私人。您可以自行指定您需要的频道。
对于API 26级,我们必须设置一个频道ID作为参考。我学会了如何在没有 channelID 的情况下执行此操作,下面是我的 firebase 消息传递设置代码。但是现在新的Androidapi26级
NotificationCompat.Builder(this);
这不起作用,我应该添加对 channelID 的引用作为
NotificationCompat.Builder(this, channelID);
但是我不知道该怎么做。 你能帮助我了解如何为 Firebase 云消息传递创建通道 ID 吗?
AndroidManifest.xml
<service android:name=".MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"></action>
</intent-filter>
</service>
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"></action>
</intent-filter>
</service>
MyFirebaseInstanceIdService Java Class:
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
private static final String REG_TOKEN = "REG_TOKEN";
@Override
public void onTokenRefresh() {
String recent_token = FirebaseInstanceId.getInstance().getToken();
Log.d(REG_TOKEN, recent_token);
}
}
MyFirebaseMessagingService Java Class
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
// Here I will set up a channel ID as "CH_ID_1" but I do not know how to do this.
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "CH_ID_1");
//****************************************************
notificationBuilder.setContentTitle("FCM NOTIFICATION");
notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
Gradle - 依赖关系
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.android.support:design:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-core:16.0.0'
implementation 'com.google.firebase:firebase-messaging:17.0.0'
}
apply plugin: 'com.google.gms.google-services'
我之前在一个应用中做的是应用一启动就初始化Notification Channels。所以我在我的应用程序 class 中添加了一个 init
函数,如下所示:
@TargetApi(Build.VERSION_CODES.O)
private void initNotificationChannels() {
NotificationChannel publicChannel = new NotificationChannel(
Constants.NOTIFICATION_CHANNEL_PUBLIC,
Constants.NOTIFICATION_CHANNEL_PUBLIC,
NotificationManager.IMPORTANCE_DEFAULT);
publicChannel.setDescription(Constants.NOTIFICATION_CHANNEL_PUBLIC);
NotificationChannel topicChannel = new NotificationChannel(
Constants.NOTIFICATION_CHANNEL_TOPIC,
Constants.NOTIFICATION_CHANNEL_TOPIC,
NotificationManager.IMPORTANCE_DEFAULT);
topicChannel.setDescription(Constants.NOTIFICATION_CHANNEL_TOPIC);
NotificationChannel privateChannel = new NotificationChannel(
Constants.NOTIFICATION_CHANNEL_PRIVATE,
Constants.NOTIFICATION_CHANNEL_PRIVATE,
NotificationManager.IMPORTANCE_HIGH);
privateChannel.setDescription(Constants.NOTIFICATION_CHANNEL_PRIVATE);
privateChannel.canShowBadge();
List<NotificationChannel> notificationChannels = new ArrayList<>();
notificationChannels.add(publicChannel);
notificationChannels.add(topicChannel);
notificationChannels.add(privateChannel);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannels(notificationChannels);
}
}
然后在我的 FirebaseMessagingService
中,我创建了一个在构建通知时获取 channelId
的函数:
private String getChannelId(String source) {
if (!TextUtils.isEmpty(source)) {
if (source.contains(Constants.TOPIC_PREFIX)) {
return (TextUtils.equals((TOPIC_PREFIX + Constants.NOTIFICATION_CHANNEL_PUBLIC), source)) ?
Constants.NOTIFICATION_CHANNEL_PUBLIC : Constants.NOTIFICATION_CHANNEL_TOPIC;
} else {
return Constants.NOTIFICATION_CHANNEL_PRIVATE;
}
} else {
return Constants.NOTIFICATION_CHANNEL_PUBLIC;
}
}
这满足了我们的应用程序需要的三种类型的通知——Public、主题和私人。您可以自行指定您需要的频道。