三星S8 / S5 / J2 / Note3成功获得Firebase推送通知应用程序被杀死,在后台或前台,
但如果应用程序被杀,Infinix Note 5(Android One-Oreo)和Oppo F9(Oreo)没有得到推送通知,如果应用程序处于后台或前台,它们可以正常工作.
我经历了很多文章,this和this,提到中国手机存在这个问题,并且用户方面有一些工作要让这些通知在他们的手机上工作.
但我想知道从开发方面是否有可能在每个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 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'