简体   繁体   English

android - Firebase 通知推送通知不起作用

[英]android - Firebase Notification Push Notification not working

UPDATE更新

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).所以我认为这是我的互联网连接问题(只是我的想法)。

I am trying to implement basic receiving of Push Notifications using Firebase.我正在尝试使用 Firebase 实现基本的推送通知接收。 I based this tutorial:我基于本教程:

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

My Problem is I don't receive any messages at all.我的问题是我根本没有收到任何消息。 I have sent more than 5 messages using the Firebase Console and still nothing happens.我使用 Firebase 控制台发送了超过 5 条消息,但仍然没有任何反应。

Here is my implementation of FirebaseInstanceIdService:这是我对 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
}

}

My FirebaseMessagingService implementation:我的 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());
}

}

My Manifest file:我的清单文件:

<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>

What am I doing wrong?我做错了什么? Or what am I lacking to implement?或者我缺乏实施什么?

I am really needing some help with this one.我真的需要一些帮助来解决这个问题。 Any help will be greatly appreciated.任何帮助将不胜感激。 Thank you very much!非常感谢!

UPDATE更新

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).所以我认为这是我的互联网连接问题(只是我的想法)。

I am trying to implement basic receiving of Push Notifications using Firebase.我正在尝试使用 Firebase 实现基本的推送通知接收。 I based this tutorial:我基于本教程:

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

My Problem is I don't receive any messages at all.我的问题是我根本没有收到任何消息。 I have sent more than 5 messages using the Firebase Console and still nothing happens.我使用 Firebase 控制台发送了超过 5 条消息,但仍然没有任何反应。

Here is my implementation of FirebaseInstanceIdService:这是我对 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
}

}

My FirebaseMessagingService implementation:我的 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());
}

}

My Manifest file:我的清单文件:

<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>

What am I doing wrong?我究竟做错了什么? Or what am I lacking to implement?或者我缺乏实施什么?

I am really needing some help with this one.我真的需要一些帮助来解决这个问题。 Any help will be greatly appreciated.任何帮助将不胜感激。 Thank you very much!非常感谢你!

In my case, every thing was OK,but the app's notification channels was inactive.就我而言,一切正常,但应用程序的通知渠道处于非活动状态。 so make sure your notification channel is active in setting.所以请确保您的通知渠道在设置中处于活动状态。

may help some one .可能会帮助一些人

onRefreshToken() deprecated, Use onNewToken(String s) onRefreshToken()已弃用, Use onNewToken(String s)

check here 在这里检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM