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

标签 android firebase google-cloud-messaging firebase-cloud-messaging

更新

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/

相关文章:

安卓蓝牙 LE : Not discovering services after connection

android - 使用 APV 实现 pdfViewer

ios - 如何将用户 Facebook 的数据添加到 Firebase

android - 通过 Intent 拍照后调用的 Activity 被杀死/onCreate

Android Things 启动 Intent

swift - Firebase 'setValue' 函数重复且不停止 Swift

ios - 从两个不同构建中的一个数组中检索项目返回不一致的结果

android - 异常 : mergeDebugResources in Android Studio

firebase - GCM3 和 FCM 向后兼容 GCM 2

android - GCMRegistrar.getRegistrationId(context) 无法获取设备 ID