gpt4 book ai didi

android - 我应该如何将通知 channel 分配给现有的 Firebase 通知类?

转载 作者:行者123 更新时间:2023-11-29 18:46:02 24 4
gpt4 key购买 nike

使用 Firebase,我向我的应用发送通知,并通过通知数据的点击操作打开某些 Activity 。问题是,它在 API 级别 <26 的情况下工作正常。但对于新版本,所有通知都必须分配给一个 channel 。但我不确定如何为现有系统分配适用于 API 级别 26 或更高级别的 channel ?

这是我的 FirebaseMessagingService java 类:

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private static final String TAG = "FirebaseMessagingServic";

public FirebaseMessagingService() {}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
try {
JSONObject data = new JSONObject(remoteMessage.getData());
String jsonMessage = data.getString("extra_information");
Log.d(TAG, "onMessageReceived: \n" +
"Extra Information: " + jsonMessage);
} catch (JSONException e) {
e.printStackTrace();
}
}

if (remoteMessage.getNotification() != null) {
String title = remoteMessage.getNotification().getTitle(); //get title
String message = remoteMessage.getNotification().getBody(); //get message
String click_action = remoteMessage.getNotification().getClickAction(); //get click_action

Log.d(TAG, "Message Notification Title: " + title);
Log.d(TAG, "Message Notification Body: " + message);
Log.d(TAG, "Message Notification click_action: " + click_action);

sendNotification(title, message, click_action);
}
}

@Override
public void onDeletedMessages() {

}

private void sendNotification(String title, String messageBody, String click_action) {
Intent intent;
if (click_action.equals("SOMEACTIVITY")) {
intent = new Intent(this, SomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
} else {
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);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 /* ID of notification */ , notificationBuilder.build());
}
}

我真的很好奇我应该在此处更改什么以将其分配给 channel ?

我还在下面分享我的 ActivityMain Activity 和 Manifest xml:

public class MainActivity {

private FirebaseAuth mAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mAuth = FirebaseAuth.getInstance();
FirebaseMessaging.getInstance().subscribeToTopic("DAILYDOSE");
}

}

list :

<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SomeActivity">
<intent-filter>
<action android:name="SOMEACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service android:name=".FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
</application>

最佳答案

要使用 channel ,您需要在创建通知之前创建一个channelId。您可以在创建服务时创建 channel ID,例如

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

private static final String CHANNEL_ID = "default";
private static final String CHANNEL_DESCRIPTION = "Your description...";

@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel();
}
}

@RequiresApi(api = Build.VERSION_CODES.O)
private void createNotificationChannel() {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_DESCRIPTION, NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}
}

这将允许您在创建通知时使用 channel ID,即

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)

关于android - 我应该如何将通知 channel 分配给现有的 Firebase 通知类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51919893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com