gpt4 book ai didi

android - Firebase 通知推送通知不工作

转载 作者:行者123 更新时间:2023-11-30 01:03:41 25 4
gpt4 key购买 nike

更新

Unfortunately, I didn't solve this issue and what I did is create a new project. Fortunately my new project works. One thing I noticed from my newly created project, when I am sending notification messages, some messages didn't arrive to my device. So I think its my Internet connection problem (my idea only).

我正在尝试使用 Firebase 实现推送通知的基本接收。我基于本教程:

https://www.codementor.io/android/tutorial/send-push-notifications-to-android-with-firebase

我的问题是我根本没有收到任何消息。我已经使用 Firebase 控制台发送了超过 5 条消息,但仍然没有任何反应。

这是我对 FirebaseInstanceIdService 的实现:

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

public static final String debugTag = "MyFirebaseIIDService";

@Override
public void onTokenRefresh() {
super.onTokenRefresh();

//Getting registration token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();

//Displaying token in logcat
Log.e(debugTag, "Refreshed token: " + refreshedToken);

}

private void sendRegistrationToServer(String token) {
//You can implement this method to store the token on your server
//Not required for current project
}

}

我的 FirebaseMessagingService 实现:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

public static final String debugTag = "MyFirebaseApp";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);

Log.d(debugTag, "Notification Received!");

//Calling method to generate notification
sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());

}

//This method is only generating push notification
private void sendNotification(String title, String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, 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, notificationBuilder.build());
}

}

我的 list 文件:

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

<service
android:name=".messagingservice.MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

<service android:name=".tokenservice.MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>

</application>

我做错了什么?或者我缺乏实现什么?

我真的需要一些帮助。任何帮助将不胜感激。非常感谢!

最佳答案

就我而言,一切正常,但应用程序的通知 channel 处于非 Activity 状态。因此请确保您的通知 channel 在设置中处于 Activity 状态。

可能对某些人有帮助

关于android - Firebase 通知推送通知不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39120987/

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