我正在尝试使用Firebase消息服务向Android 12设备发送推送通知。当我尝试从nodeJs发送FCM时,onMessageReceived未调用。我尝试从Firebase控制台发送FCM,但即使应用程序是在后台的应用程序是不显示任何通知或错误。我已经测试了这个应用程序在android 11和android 10**并且两者都工作良好。
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/icon"
android:label="@string/app_name"
android:roundIcon="@mipmap/icon_round"
android:supportsRtl="true"
android:theme="@style/Theme.MobileSMSService"
android:usesCleartextTraffic="true">
<service
android:name=".helper.MessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/sms_icon_foreground" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/purple_700" />
</service>
</application>
MessagingService.java
public class MessagingService extends FirebaseMessagingService {
private static final String CHANNEL_ID = "FCM";
private static final String TAG = "MessagingService";
whatsappMessageService messageService;
@Override
public void onNewToken(@NonNull String s) {
super.onNewToken(s);
JSONObject obj = new JSONObject();
SharedPreferences sp = getSharedPreferences("Login", MODE_PRIVATE);
String api = sp.getString("api", null);
sp.edit().putString("token", s).apply();
try {
obj.put("token", s);
obj.put("api", api);
new Server().post(new Server().baseUrl + "update-token", obj.toString(), new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
}
});
} catch (Exception e) {
Log.e("MessagingService", "onNewToken: ", e);
}
}
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.e(TAG, "onMessageReceived: recived" );
Map<String, String> data = remoteMessage.getData();
if (data.get("web") == null) {
String message = data.get("message"), to = data.get("to"), plat = data.get("plat");
if (plat != null) {
if (plat.equals("w")) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
boolean isScreenOnApi21 = pm.isInteractive();
if (!isScreenOn | !isScreenOnApi21) {
SharedPreferences sp = getApplicationContext().getSharedPreferences("Login", MODE_PRIVATE);
if (sp.getString("wake_phone", null) != null) {
sendSMS(sp.getString("wake_phone", null), "Wake up");
new MessagesDatabaseHelper(this).insertMessage(sp.getString("wake_phone", null), "Wake up");
try {
Thread.sleep(3500);
isScreenOn = pm.isScreenOn();
isScreenOnApi21 = pm.isInteractive();
if (!isScreenOn | !isScreenOnApi21) {
Thread.sleep(3500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
return;
}
}
if (messageService == null) {
messageService = new whatsappMessageService(getApplicationContext());
messageService.start();
}
messageService.addMessage(to, message);
new WhatsappDatabaseHelper(this).insertMessage(to, message);
} else {
new MessagesDatabaseHelper(this).insertMessage(to, message);
sendSMS(to, message);
}
Intent intents = new Intent();
intents.setAction("sms_receiver");
getBaseContext().sendBroadcast(intents);
}
} else {
Log.e(TAG, "onMessageReceived: " + "web");
new webManager().onMessage(data);
}
}
2条答案
按热度按时间mfpqipee1#
您好,您的清单文件的格式不是很好,请检查下面的清单,并进行相应的调整,也请确保您有一个
FirebaseMessagingService
。还要检查您的build.gradle并添加以下依赖项的左大括号
FirebaseMessagingService应采用以下格式:
rqqzpn5f2#
对于新来的人谁可能会来这个问题,我有完全相同的问题:
在我的例子中,我仍然使用Firebase库17.x和过时的架构。
现在我把firebase依赖项更新到了23.0.4版,它运行得很好。