[英]Some Oreo devices are not getting Push Notification
三星S8/S5/J2/Note3
Firebase Push Notification
成功獲得了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'
如果應用程序被殺死,Infinix Note 5(Android One-Oreo)和Oppo F9(Oreo)不會收到推送通知,如果應用程序在后台或前景中,它們都可以正常工作。
從最近的應用程序托盤中刷過應用程序后,使用(氧氣操作系統,MIUI等)的中文ROM會終止(類似於強制停止)。 由於這個原因,每項任務都會在后台運行,例如服務,因此喬布斯會被該應用殺死。 即使是高優先級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);
}
}
}
這是所有中文設備的問題。一旦您殺死了該應用程序...它將被從內存中刪除,並且不是所有處於活動狀態...但是對於其他設備制造商而言,情況並非如此..它們不會被刪除該應用程序完全來自內存。 我也遇到了小米設備的問題....他們也可以選擇鎖定應用程序,即使將其殺死以清除ram內存也可以將應用程序保留在內存中
由於oreo,有了新的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());
我剛剛創建了通知頻道,檢查Oreo的條件。
如果您有任何問題,請告訴我。 我是來幫你的
謝謝。
我遇到了同樣的問題,經過搜索和解決后,我發現用戶需要手動啟用自動啟動和電池優化權限,有關更多詳細信息,請參閱此鏈接
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.