Android 无法在设备上获取令牌和FCM通知
我正在我的应用程序中集成FCM。已从Firebase发送FCM,但未在设备上显示。我正在尝试在我的应用程序前台和后台显示通知 1-:将应用程序连接到Firebase并将FCM添加到我的应用程序。(Android Studio-->工具-->Firebase-->云消息) 2-:创建MyFireBaseMessageService1类,并通过FireBaseMessageService和实现方法(onNewToken和onMessageReceived)进行扩展 3-:在onMessageReceived中创建方法genrateNotification 4-:在清单文件中添加此服务并添加操作com.google.firebase.MESSAGING\u事件Android 无法在设备上获取令牌和FCM通知,android,firebase,push-notification,firebase-cloud-messaging,Android,Firebase,Push Notification,Firebase Cloud Messaging,我正在我的应用程序中集成FCM。已从Firebase发送FCM,但未在设备上显示。我正在尝试在我的应用程序前台和后台显示通知 1-:将应用程序连接到Firebase并将FCM添加到我的应用程序。(Android Studio-->工具-->Firebase-->云消息) 2-:创建MyFireBaseMessageService1类,并通过FireBaseMessageService和实现方法(onNewToken和onMessageReceived)进行扩展 3-:在onMessageRece
private void genrateNotification(String body, String title) {
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 soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
if(NOTIFICATION_ID > 1073741824){
NOTIFICATION_ID =0;
}
notificationManager.notify(NOTIFICATION_ID++,notificationBuilder.build());
}
在执行上述代码后未获取令牌和通知在检索令牌之前,请确保:
- 您的设备已连接到Internet李>
- 应用程序已成功连接到firebase李>
- 您已经在设备上安装了最新的
GooglePlayServices
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "getInstanceId failed", task.getException());
return;
}
// Get new Instance ID token
String token = task.getResult().getToken();
// Log and toast
String msg = getString(R.string.msg_token_fmt, token);
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
在您的舱单中:
<service
android:name=".MyNotificationService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
通过登录onMessageReceived()方法进行检查。首先检查你的设置。将通知优先级设为t高,然后在Manifest.NotificationCompat.Bulider中检查INTERNET Premission I仅传递此not通道id。错误:类生成器中的构造函数生成器无法应用于给定类型;请看这里,我的编译和目标sdk版本是28
<service
android:name=".MyNotificationService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>