android FCM推送通知在一加6移动的不工作

sxissh06  于 2023-06-27  发布在  Android
关注(0)|答案(4)|浏览(235)

当设备处于后台、前台以及通过从托盘滑动关闭应用程序时,FCM推送通知在以下设备中正常工作。品牌(安卓版)Micromax(5.1)摩托罗拉(7.1.1)诺基亚(8.1.0)三星(8.0.0)Nexus(8.1.0)小米(7.1.2)

**但在OnePlus的情况下,当通过从托盘滑动关闭应用程序时,fcm通知不起作用,但当应用程序处于前台和后台时,fcm通知可以正常工作。
但当我手动关闭我的应用程序的电池优化选项,然后在所有情况下fcm推送通知在Oneplus设备中正常工作

我的androidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. package="com.demo.Notification"
  5. android:installLocation="auto">
  6. <uses-permission android:name="android.permission.VIBRATE" />
  7. <application
  8. android:allowBackup="false"
  9. android:icon="@mipmap/ic_launcher"
  10. android:label="@string/app_name"
  11. android:theme="@style/AppTheme">
  12. <!-- [START fcm_default_icon] -->
  13. <!-- Set custom default icon. This is used when no icon is set for incoming notification messages. -->
  14. <meta-data
  15. android:name="com.google.firebase.messaging.default_notification_icon"
  16. android:resource="@mipmap/ic_launcher" />
  17. <!-- [END fcm_default_icon] -->
  18. <!-- [START fcm_default_channel] -->
  19. <meta-data
  20. android:name="com.google.firebase.messaging.default_notification_channel_id"
  21. android:value="@string/default_notification_channel_id"/>
  22. <!-- [END fcm_default_channel] -->
  23. <service
  24. android:name=".MyFirebaseMessagingService">
  25. <intent-filter>
  26. <action android:name="com.google.firebase.MESSAGING_EVENT"/>
  27. </intent-filter>
  28. </service>
  29. <activity
  30. android:name="com.demo.Notification.MainActivity">
  31. <intent-filter>
  32. <action android:name="android.intent.action.MAIN" />
  33. <category android:name="android.intent.category.LAUNCHER" />
  34. </intent-filter>
  35. </activity>
  36. </application>
  37. </manifest>

我的MyFirebaseMessagingService.java

  1. package com.demo.Notification;
  2. import android.app.NotificationChannel;
  3. import android.app.NotificationManager;
  4. import android.app.PendingIntent;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.graphics.Bitmap;
  8. import android.graphics.BitmapFactory;
  9. import android.graphics.Color;
  10. import android.media.RingtoneManager;
  11. import android.net.Uri;
  12. import android.os.Build;
  13. import android.support.annotation.RequiresApi;
  14. import android.support.v4.app.NotificationCompat;
  15. import android.support.v4.app.NotificationManagerCompat;
  16. import com.google.firebase.messaging.FirebaseMessagingService;
  17. import com.google.firebase.messaging.RemoteMessage;
  18. import org.json.JSONObject;
  19. public class MyFirebaseMessagingService extends FirebaseMessagingService
  20. {
  21. private static final String NOTIFICATION_MESSAGE_KEY = "MESSAGE";
  22. private NotificationManager notificationManager;
  23. @Override
  24. public void onMessageReceived(RemoteMessage remoteMessage)
  25. {
  26. sendNotification(remoteMessage.getData().get(NOTIFICATION_MESSAGE_KEY));
  27. }
  28. private void sendNotification(String msg)
  29. {
  30. String notification_message_title = "";
  31. String notification_message_text = "";
  32. int notification_id = 1;
  33. String channel_id = getString(R.string.default_notification_channel_id);
  34. try
  35. {
  36. JSONObject jsonObject = new JSONObject(msg);
  37. if(jsonObject.has("notification_message_title"))
  38. {
  39. notification_message_title = jsonObject.getString("notification_message_title");
  40. notification_message_title = (notification_message_title != null) ? notification_message_title.trim() : "";
  41. }
  42. if(jsonObject.has("notification_message_text"))
  43. {
  44. notification_message_text = jsonObject.getString("notification_message_text");
  45. notification_message_text = (notification_message_text != null) ? notification_message_text.trim() : "";
  46. }
  47. if("".equals(notification_message_title))
  48. {
  49. return;
  50. }
  51. if("".equals(notification_message_text))
  52. {
  53. return;
  54. }
  55. }
  56. catch(Exception e)
  57. {
  58. return;
  59. }
  60. notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  61. if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
  62. {
  63. setupChannels();
  64. }
  65. NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this,channel_id);
  66. mBuilder.setAutoCancel(true);
  67. mBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
  68. mBuilder.setContentTitle(notification_message_title);
  69. mBuilder.setContentText(notification_message_text);
  70. mBuilder.setColor(Color.BLUE);
  71. mBuilder.setSmallIcon(R.mipmap.ic_launcher);
  72. Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
  73. mBuilder.setLargeIcon(largeIcon);
  74. Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  75. mBuilder.setSound(notificationSound);
  76. Intent resultIntent = new Intent(this, MainActivity.class);
  77. resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  78. PendingIntent resultPendingIntent =
  79. PendingIntent.getActivity(
  80. this,
  81. notification_id,
  82. resultIntent,
  83. PendingIntent.FLAG_UPDATE_CURRENT
  84. );
  85. mBuilder.setContentIntent(resultPendingIntent);
  86. if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
  87. {
  88. mBuilder.setChannelId(channel_id);
  89. }
  90. NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
  91. notificationManagerCompat.notify(notification_id, mBuilder.build());
  92. //SEND Notification END
  93. }
  94. @RequiresApi(api = Build.VERSION_CODES.O)
  95. private void setupChannels(){
  96. String channel_id = getString(R.string.default_notification_channel_id);
  97. CharSequence channelName = getString(R.string.default_notification_channel_name);
  98. NotificationChannel channel = new NotificationChannel(channel_id, channelName, NotificationManager.IMPORTANCE_MAX);
  99. channel.enableLights(true);
  100. channel.setLightColor(Color.BLUE);
  101. channel.enableVibration(true);
  102. if (notificationManager != null) {
  103. notificationManager.createNotificationChannel(channel);
  104. }
  105. }
  106. }

我的应用级别build.gradel

  1. apply plugin: 'com.android.application'
  2. android {
  3. compileSdkVersion 27
  4. buildToolsVersion "27.0.3"
  5. defaultConfig {
  6. applicationId "com.demo.Notification"
  7. minSdkVersion 19
  8. targetSdkVersion 27
  9. versionCode 1
  10. versionName "1.0.0"
  11. }
  12. buildTypes {
  13. release {
  14. minifyEnabled true
  15. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  16. }
  17. }
  18. sourceSets { main { assets.srcDirs = ['src/main/assets', 'src/main/assets/'] } }
  19. }
  20. dependencies {
  21. compile fileTree(dir: 'libs', include: ['*.jar'])
  22. compile 'com.android.support:appcompat-v7:27.1.1'
  23. compile 'com.google.code.gson:gson:2.2.4'
  24. compile 'com.google.firebase:firebase-messaging:17.3.0'
  25. }
  26. apply plugin: 'com.google.gms.google-services'

我的项目级别构建.gradle

  1. buildscript {
  2. repositories {
  3. jcenter()
  4. maven {
  5. url "https://maven.google.com"
  6. }
  7. }
  8. dependencies {
  9. classpath 'com.android.tools.build:gradle:2.2.3'
  10. classpath 'com.google.gms:google-services:4.1.0'
  11. }
  12. }
  13. allprojects {
  14. repositories {
  15. jcenter()
  16. maven {
  17. url "https://maven.google.com"
  18. }
  19. }
  20. }

我用这种方式向服务器发送令牌

  1. public void registerDevice()
  2. {
  3. FirebaseInstanceId.getInstance().getInstanceId()
  4. .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
  5. @Override
  6. public void onComplete(@NonNull Task<InstanceIdResult> task)
  7. {
  8. String registrationId = task.getResult().getToken();
  9. sendTokenToServer(registrationId);
  10. }
  11. });
  12. }

任何小的帮助将不胜感激

falq053o

falq053o1#

这是因为**Doze Mode你可以在fcm中通过设置来自后端的推送通知消息优先级为高消息优先级来克服这个问题看看是documentation**

mfpqipee

mfpqipee2#

在这些设备(如OnePlus,Huawie,OPPO)中,他们正在使用基于Android操作系统的自定义版本的操作系统,可能是在电池优化时,它强制关闭FCM的后台服务,我们没有收到任何通知。

c6ubokkw

c6ubokkw3#

如果在Android Studio中测试应用时,FCM推送通知未专门在OnePlus 6设备上工作,您可以按照以下几个步骤来解决问题:
1.检查FCM实现:确保您在Android Studio项目中正确实现了FCM。验证您是否已在应用的build.gradle文件中包含必要的依赖项,并在Firebase中注册了应用。
1.验证设备令牌:确保服务器正确接收FCM生成的设备令牌。您可以在应用代码中记录令牌,并验证令牌是否正在生成并成功发送到服务器。
1.启用调试模式:在Android Studio中为您的应用启用调试模式。您可以通过将以下代码行添加到应用的onCreate()方法来实现此目的:

  1. FirebaseMessaging.getInstance().setAutoInitEnabled(true);

启用调试模式后,即使您的应用处于前台,您也可以接收FCM消息,这有助于测试和故障排除。
1.检查消息有效负载:确保发送的FCM消息具有正确的有效负载结构。确保“to”字段包含OnePlus 6设备的FCM令牌,并且有效载荷包括通知所需的数据字段。
1.其他器械测试:尝试在其他设备上测试FCM推送通知,以验证该问题是否特定于OnePlus 6或是否发生在多个设备上。这可以帮助缩小问题的范围,并确定它是否与设备或您的实现相关。
1.检查通知设置:在OnePlus 6设备上,转到“设置”
应用程序和通知> [您的应用程序] >通知。确保为您的应用启用了通知。此外,检查是否有任何特定设置可能会阻止或覆盖通知的显示。
1.更新设备软件:确保您的OnePlus 6设备正在运行最新的软件更新。过时的固件有时会导致与FCM的兼容性问题。通过转到“设置”检查更新
系统>系统更新。
1.重置FCM令牌:如果所有这些都失败,您可以尝试在OnePlus 6设备上重置FCM令牌。从设备卸载应用程序,清除所有缓存的数据,然后重新安装应用程序。这将为设备生成一个新的FCM令牌。
如果在执行这些步骤后问题仍然存在,您可能需要进一步调查或寻求OnePlus支持团队或Firebase支持的帮助,因为他们可以根据您的情况提供更具体的指导。

展开查看全部
sycxhyv7

sycxhyv74#

试试这个代码

  1. Uri defaultSoundUri =
  2. RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  3. NotificationCompat.Builder notificationBuilder = new
  4. NotificationCompat.Builder(this)
  5. .setSmallIcon(R.drawable.ic_notif_icon)
  6. .setContentTitle("testTitle")
  7. .setContentText(messageBody)
  8. .setAutoCancel(true)
  9. .setSound(defaultSoundUri)
  10. .setChannelId("testChannelId") // set channel id
  11. .setContentIntent(pendingIntent);

相关问题