三星S8/S5/J2/Note3
成功Firebase Push Notification
应用程序被杀死,无论是在后台还是前台,
但是,如果应用程序被杀死,Infinix Note 5 (Android One- Oreo)
和Oppo F9(Oreo)
不会收到推送通知,但如果应用程序在后台或前台,它们工作正常。
我浏览了很多文章,这个和这个,提到中国手机有这个问题,并且用户方面有一些解决方法可以使这些通知在他们的手机上工作。
但我想知道从开发方面是否有可能使通知在每个 Android 设备上工作。
我的目标是 API 27,这是我的代码
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String from = remoteMessage.getFrom();
Map data = remoteMessage.getData();
JSONObject jsonObject = new JSONObject();
Set<String> keys = remoteMessage.getData().keySet();
for (String key : keys) {
try {
jsonObject.put(key, remoteMessage.getData().get(key));
} catch (JSONException e) {
e.printStackTrace();
}
}
message= jsonObject.getString("message");
sendNotification(message, urlToOpen);
private void sendNotification(final String msg, final String urlToOpen) {
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);
String channelId = getString(R.string.notification_channel_general);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("App Name")
.setContentText(msg)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"App Channel",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build());
格拉德尔
compileSdkVersion 27
defaultConfig {
applicationId "com.myapp.app"
minSdkVersion 19
targetSdkVersion 27
versionCode 2
versionName "1"
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
// Disable fabric build ID generation for debug builds
ext.enableCrashlytics = false
}
}
implementation 'com.google.firebase:firebase-messaging:15.0.0'
implementation 'com.google.android.gms:play-services-location:15.0.0'
implementation 'com.google.android.gms:play-services-auth:15.0.0'
implementation 'com.google.android.gms:play-services-basement:16.0.1'
implementation 'com.google.android.gms:play-services-ads-identifier:16.0.0'
implementation 'com.google.android.gms:play-services-stats:16.0.1'
implementation 'com.google.android.gms:play-services-tasks:16.0.1'
implementation 'com.google.android.gms:play-services-ads-identifier:16.0.0'
implementation 'com.google.android.gms:play-services-ads-identifier:16.0.0'
implementation 'com.google.android.gms:play-services-maps:16.0.0'
来源:在中文ROM中启用后台服务和作业(或FCM(
Infinix Note 5(Android One-Oreo(和Oppo F9(Oreo(在应用程序被杀死时不会收到推送通知,如果应用程序在后台或前台,它们工作正常。
使用中文 ROM(Oxygen OS、MIUI 等(当应用程序从最近的应用程序托盘中滑动时,您的应用程序将被终止(类似于强制停止(。由于这个原因,每个在后台运行的任务,如服务,乔布斯都会被应用程序杀死。即使是高优先级FCM在中国ROM中也看不到日光
实时问题:
1( 您无法获取用户的位置。因此,如果依赖于实时位置,则任何应用程序都无法正常工作
2( 您无法收到 FCM 高优先级通知
3(如果应用程序不在托盘中,则没有时间特定的任务/作业将执行,还有更多....
溶液
用户需要在应用程序的设置中启用自动启动,以保持后台服务/作业运行。默认情况下,它处于关闭状态。要在我们可以使用的某些设备中执行此操作,
示例代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent();
String manufacturer = android.os.Build.MANUFACTURER;
switch (manufacturer) {
case "xiaomi":
intent.setComponent(new ComponentName("com.miui.securitycenter",
"com.miui.permcenter.autostart.AutoStartManagementActivity"));
break;
case "oppo":
intent.setComponent(new ComponentName("com.coloros.safecenter",
"com.coloros.safecenter.permission.startup.StartupAppListActivity"));
break;
case "vivo":
intent.setComponent(new ComponentName("com.vivo.permissionmanager",
"com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
break;
}
List<ResolveInfo> arrayList = getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
if (arrayList.size() > 0) {
startActivity(intent);
}
}
}
在奥利奥,他们引入了推送通知通道化的新概念。您可以在此处阅读有关通道化的更多信息。分别实施奥利奥和波纹管矿石上方的通知。您可以参考以下答案来解决此问题。
我已经在xamarin.android中解决了这个问题,希望这对你有用。
这是所有中国设备的问题。一旦你杀死了应用程序...它从内存中删除,而不是全部处于活动状态...但在其他设备制造商的情况下,情况并非如此..他们不会从内存中完全删除该应用程序。我也面临着小米设备的问题....他们有一个选项来锁定应用程序,即使它被杀死以清除RAM内存,也会将应用程序保留在内存中
自奥利奥以来,有了新的Notification Channels
实现(始于Android 8.0(API级别26((
所以你必须使用以下代码创建通道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "News Channel";
String description = "Default Notification News Channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getAppContext().getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
并覆盖通知构建器,如下所示
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getAppContext(),CHANNEL_ID);
假设你有CHANNEL_ID价值你想要的东西
String CHANNEL_ID = "DEFAULT_NEWS_CHANNEL";
这已经在OnePlus3T设备上进行了测试,因此它应该适合您
我希望以下代码对您有用。它在每种设备中都为我工作。
Intent notificationIntent = new Intent(context, SplashScreenActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence nameChannel = context.getString(R.string.app_name);
String descChannel = context.getString(R.string.app_name);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(context.getString(R.string.app_name), nameChannel, importance);
channel.setDescription(descChannel);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
assert notificationManager != null;
notificationManager.createNotificationChannel(channel);
}
PendingIntent pendingIntent = PendingIntent.getActivity((context), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Create Notification
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, context.getString(R.string.app_name))
.setChannelId(context.getString(R.string.app_name))
.setContentTitle(TextUtils.isEmpty(title) ? getString(R.string.app_name) : title)
.setContentText(description)
.setTicker(context.getString(R.string.app_name))
.setSmallIcon(R.drawable.ic_stat_name)
.setSound(notificationSound)
.setLights(Color.RED, 3000, 3000)
.setVibrate(new long[]{500, 500})
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
if (result != null) {
notification.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(result));
}
assert notificationManager != null;
notificationManager.notify(100, notification.build());
我刚刚创建了通知通道,检查奥利奥的条件。
如果您有任何问题,请告诉我。我是来帮你的。
谢谢。
我遇到了同样的问题,经过搜索和解决, 我发现用户需要手动启用自动启动和电池优化权限 有关更多详细信息,请参阅此链接