我有获得推送通知的问题,Gionee手机应用程序从后台清除,即刷出内存。我正在使用FCM技术。它是完美的工作(当应用程序在后台运行时和应用程序从内存中清除时)在其他手机中,如三星,微型,熔岩等。我不知道为什么应用程序没有'允许在Honor phones.Code中提供推送通知,以供您参考。
MyFirebaseInstanceIDService.java
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = MyFirebaseInstanceIDService.class.getSimpleName();
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
storeRegIdInPref(refreshedToken);
// sending reg id to your server
sendRegistrationToServer(refreshedToken);
// Notify UI that registration has completed, so the progress indicator can be hidden.
Intent registrationComplete = new Intent(Config.REGISTRATION_COMPLETE);
registrationComplete.putExtra("token", refreshedToken);
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
private void sendRegistrationToServer(final String token) {
// sending gcm token to server
// Log.e("token" ,token);
}
private void storeRegIdInPref(String token) {
SharedPreferences pref = getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0);
SharedPreferences.Editor editor = pref.edit();
editor.putString("regId", token);
editor.commit();
}
}
MyFirebaseMessagingService .java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
private NotificationUtils notificationUtils;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage == null)
return;
if (remoteMessage.getNotification() != null) {
handleNotification(remoteMessage.getNotification().getBody());
}
if (remoteMessage.getData().size() > 0) {
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
handleDataMessage(json);
} catch (Exception e) {
}
}
}
private void handleNotification(String message) {
if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
Intent intent = new Intent( this , MainActivity.class);
intent.putExtra("message", message);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIntent = PendingIntent.getActivity(this , 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(message)
.setAutoCancel(true)
.setSound(notificationSoundURI)
.setContentIntent(resultIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotificationBuilder.build());
}else{
}
}
private void handleDataMessage(JSONObject json) {
try {
JSONObject data = json.getJSONObject("data");
String title = data.getString("title");
String message = data.getString("message");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
Intent intent;
intent = new Intent(this, MainActivity.class);
int uid = 0;
Date dNow = new Date();
SimpleDateFormat ft = new SimpleDateFormat("hhmmssMs");
String datetime = ft.format(dNow);
try {
uid = Integer.parseInt(datetime);
} catch (NumberFormatException e) {
e.printStackTrace();
uid = 0;
}
intent.putExtra("message", message);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(uid,PendingIntent.FLAG_ONE_SHOT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(this);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mNotificationBuilder
.setSmallIcon(R.mipmap.ic_notification)
.setTicker(title)
.setWhen(System.currentTimeMillis())
.setAutoCancel( true )
.setContentTitle(/*getResources().getString(R.string.app_name)*/title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentIntent(resultPendingIntent/*resultIntent*/)
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorAccent))
.setContentText(message)
.setSound(notificationSoundURI);
} else {
mNotificationBuilder
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(/*getResources().getString(R.string.app_name)*/title)
.setContentText(message)
.setWhen(System.currentTimeMillis())
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setAutoCancel( true )
.setSound(notificationSoundURI)
.setContentIntent(resultPendingIntent/*resultIntent*/);
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(uid, mNotificationBuilder.build());
} catch (JSONException e) {
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) {
notificationUtils = new NotificationUtils(context);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtils.showNotificationMessage(title, message, timeStamp, intent);
}
private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl) {
notificationUtils = new NotificationUtils(context);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl);
}
}
请提出一些建议。提前谢谢。