当设备处于后台、前台以及通过从托盘滑动关闭应用程序时,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
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.demo.Notification"
android:installLocation="auto">
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<!-- [START fcm_default_icon] -->
<!-- Set custom default icon. This is used when no icon is set for incoming notification messages. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@mipmap/ic_launcher" />
<!-- [END fcm_default_icon] -->
<!-- [START fcm_default_channel] -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id"/>
<!-- [END fcm_default_channel] -->
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<activity
android:name="com.demo.Notification.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我的MyFirebaseMessagingService.java
package com.demo.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import org.json.JSONObject;
public class MyFirebaseMessagingService extends FirebaseMessagingService
{
private static final String NOTIFICATION_MESSAGE_KEY = "MESSAGE";
private NotificationManager notificationManager;
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
sendNotification(remoteMessage.getData().get(NOTIFICATION_MESSAGE_KEY));
}
private void sendNotification(String msg)
{
String notification_message_title = "";
String notification_message_text = "";
int notification_id = 1;
String channel_id = getString(R.string.default_notification_channel_id);
try
{
JSONObject jsonObject = new JSONObject(msg);
if(jsonObject.has("notification_message_title"))
{
notification_message_title = jsonObject.getString("notification_message_title");
notification_message_title = (notification_message_title != null) ? notification_message_title.trim() : "";
}
if(jsonObject.has("notification_message_text"))
{
notification_message_text = jsonObject.getString("notification_message_text");
notification_message_text = (notification_message_text != null) ? notification_message_text.trim() : "";
}
if("".equals(notification_message_title))
{
return;
}
if("".equals(notification_message_text))
{
return;
}
}
catch(Exception e)
{
return;
}
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
setupChannels();
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this,channel_id);
mBuilder.setAutoCancel(true);
mBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
mBuilder.setContentTitle(notification_message_title);
mBuilder.setContentText(notification_message_text);
mBuilder.setColor(Color.BLUE);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
mBuilder.setLargeIcon(largeIcon);
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(notificationSound);
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
notification_id,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
mBuilder.setChannelId(channel_id);
}
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(notification_id, mBuilder.build());
//SEND Notification END
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void setupChannels(){
String channel_id = getString(R.string.default_notification_channel_id);
CharSequence channelName = getString(R.string.default_notification_channel_name);
NotificationChannel channel = new NotificationChannel(channel_id, channelName, NotificationManager.IMPORTANCE_MAX);
channel.enableLights(true);
channel.setLightColor(Color.BLUE);
channel.enableVibration(true);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
}
我的应用级别build.gradel
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
defaultConfig {
applicationId "com.demo.Notification"
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0.0"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets { main { assets.srcDirs = ['src/main/assets', 'src/main/assets/'] } }
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:27.1.1'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.google.firebase:firebase-messaging:17.3.0'
}
apply plugin: 'com.google.gms.google-services'
我的项目级别构建.gradle
buildscript {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.google.gms:google-services:4.1.0'
}
}
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
我用这种方式向服务器发送令牌
public void registerDevice()
{
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task)
{
String registrationId = task.getResult().getToken();
sendTokenToServer(registrationId);
}
});
}
任何小的帮助将不胜感激
4条答案
按热度按时间falq053o1#
这是因为**Doze Mode你可以在fcm中通过设置来自后端的推送通知消息优先级为高消息优先级来克服这个问题看看是documentation**
mfpqipee2#
在这些设备(如OnePlus,Huawie,OPPO)中,他们正在使用基于Android操作系统的自定义版本的操作系统,可能是在电池优化时,它强制关闭FCM的后台服务,我们没有收到任何通知。
c6ubokkw3#
如果在Android Studio中测试应用时,FCM推送通知未专门在OnePlus 6设备上工作,您可以按照以下几个步骤来解决问题:
1.检查FCM实现:确保您在Android Studio项目中正确实现了FCM。验证您是否已在应用的build.gradle文件中包含必要的依赖项,并在Firebase中注册了应用。
1.验证设备令牌:确保服务器正确接收FCM生成的设备令牌。您可以在应用代码中记录令牌,并验证令牌是否正在生成并成功发送到服务器。
1.启用调试模式:在Android Studio中为您的应用启用调试模式。您可以通过将以下代码行添加到应用的onCreate()方法来实现此目的:
启用调试模式后,即使您的应用处于前台,您也可以接收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支持的帮助,因为他们可以根据您的情况提供更具体的指导。
sycxhyv74#
试试这个代码