gpt4 book ai didi

android - Android O 中未设置 Firebase Cloud Messaging 后台通知 channel ID

转载 作者:行者123 更新时间:2023-11-29 16:50:05 26 4
gpt4 key购买 nike

Firebase 似乎没有在传入消息上设置通知 channelId 到 android M。我正在关注 this guide ,并尝试在我的应用程序处于后台时触发通知。

这是我的应用程序代码和 list 。

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>

如您所见,我确实在注册我的通知 channel ,正如它所说的 here .我设备上的这张屏幕截图确认 channel 也已注册:

enter image description here

这是关键部分。 当我以 API 版本 25 为目标时,我会收到后台通知。一旦我以版本 26 为目标,我就不会。 相反,我在 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)

我肯定会设置一个 channel ID,如从 Firebase 控制台发送的屏幕截图所示:

enter image description here

我也试过通过 HTTP 服务发送:

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\"}}"

在这两种情况下,消息都会到达我的设备,但没有 channelId(如上面日志消息中的 channelId=null 所示)。

在前台,我可以通过服务捕获通知并手动在其上粘贴一个 channel ID(即使我必须在消息的有效负载中对​​ channel ID 进行编码)。但我也需要后台通知才能正常工作,据我所知,这取决于 Firebase 库。

最佳答案

Firebase Release Notes表示在版本 10.2.6(2017 年 5 月 17 日)中添加了对通知 channel 的支持:

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.

更新您的构建以使用版本 10.2.6 或更高版本。

关于android - Android O 中未设置 Firebase Cloud Messaging 后台通知 channel ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46694100/

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