android Firebase云消息传递不起作用尝试了一切

xvw2m8pv  于 2023-04-18  发布在  Android
关注(0)|答案(1)|浏览(134)

应用程序清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.urvish.notificationdemo">
<uses-permission android:name="android.permission.INTERNET"/>
<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>
    <service android:name=".MyFirebaseInstanceIdService" android:enabled="true" android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>
    <service android:name=".MyFirebaseMessagingService" android:enabled="true" android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/ic_android" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="my_channel_01" />
</application>

MainActivity.java

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

MyFirebaseInstanceIdService.java

public class MyFirebaseInstanceIdService extends 
FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
        super.onTokenRefresh();
        String token = FirebaseInstanceId.getInstance().getToken();
        Log.d("MyToken",token);
    }
 }

MyFirebaseMessagingService.java

public class MyFirebaseMessagingService extends 
FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        String title = remoteMessage.getNotification().getTitle();
        String body = remoteMessage.getNotification().getBody();
        Log.d("TAG", "onMessageReceived: " + title + ":" + body);
        System.out.println("msg" + title + ":" + body);
    }
}

到目前为止,我检查了我的应用程序连接到firebase。我最初得到了令牌。但仍然,我没有收到来自firbase cloud的任何通知。我已经阅读了文档并遵循了教程。

pjngdqdw

pjngdqdw1#

我也遇到了同样的问题,我先取回了令牌,最后我成功地收到了消息。希望这会有帮助。

FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(new 
 OnCompleteListener<InstanceIdResult>() {
        @Override
        public void onComplete(@NonNull Task<InstanceIdResult> task) {
            if (!task.isSuccessful()) {

                return;
            }

            // Get new Instance ID token
            String token = task.getResult().getToken();

            // Log and toast
            String msg = getString(R.string.msg_token_fmt, token);

            Toast.makeText(Login.this, msg, Toast.LENGTH_SHORT).show();
        }
    });

相关问题