我在使用 FireBase Cloud Messaging 时遇到问题,我从设备获取令牌并通过 Google Firebase 通知控制台发送通知测试,但是,通知从未被记录或推送到 android 虚拟设备。FCM 的文档几乎就是我在下面的代码,而在使用 firebase 时,您还需要做些什么来获得推送通知。我已经完成了文档中指定的所有设置信息(build.gradle 添加、安装 google play 服务等),但仍然没有生成消息。在推送“I/FA:未找到跟踪代码管理器,因此不会使用”的通知后,我确实收到了一次性错误 但这是唯一输出的数据,我在谷歌上没有找到任何与跟踪代码管理器要求和 FCM 相关的内容。我没有收到到 logcat 或设备的推送通知的代码有什么问题?请让我知道任何有帮助的进一步信息。谢谢。
NotificationGenie.java
public class NotificationGenie extends FirebaseMessagingService {
private static final String TAG = "Firebase_MSG";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
sendNotification(remoteMessage.getNotification().getBody());
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, PostLoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.tf2spyprofile)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.test.movile_android">
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<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=".LoginActivity"
android:label="@string/title_activity_login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DashBoardActivity"
android:label="@string/title_activity_dash_board"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".NewOrderActivity"
android:label="@string/title_activity_dash_board"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".PostLoginActivity"
android:label="@string/title_activity_dash_board"
android:theme="@style/AppTheme.NoActionBar">
</activity>
</application>
<service
android:name=".NotificationGenie">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>