我在应用中收到的(Firebase 推送(通知遇到了此问题。它们只出现一秒钟,然后消失。我真的想不通我做错了什么。这是我的代码:
public class PushNotification extends FirebaseMessagingService
{
private Gson gson = new Gson();
@Override
public void onMessageReceived(final RemoteMessage remoteMessage){
String TAG = PushNotification.this.getClass().getName();
Log.e("PushNotification", "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0)
{
Log.d("PushNotification", "Notification Received");
for (String key : remoteMessage.getData().keySet())
{
Object value = remoteMessage.getData().get(key);
String temp = String.format("%s %s (%s)", key, value.toString(),
value.getClass().getName());
Log.d("PushNotification", "key: "+ temp);
}
}
else Log.d("PushNotification", "NO DATA PAYLOAD");
methodToHandleNotifications(remoteMessage.getData());
}
private void methodToHandleNotifications(Map<String, String> data){
CarInfo mCarInfo =
gson.fromJson(data.get(Tags.PUSH_NOTIFICATION_KEY_CAR_INFO), CarInfo.class);
Intent resultIntent = new
Intent(AppController.getInstance().getApplicationContext(), MyActivity.class);
resultIntent.putExtra("car",
data.get(Tags.PUSH_NOTIFICATION_KEY_CAR_INFO));
resultIntent.putExtra("id", mCarInfo.getId());
resultIntent.putExtra("position", 0);
showNotificationMessage(AppController.getInstance().getApplicationContext(),
AppController.getInstance().getResources().getString(R.string.notification_auction
_ticker_from_own_company),
AppController.getInstance()
.getResources()
.getString(R.string.notification_auction_content,
mCarInfo.getFullName()),
BuildConfig.URL_API_IMAGES + mCarInfo.getPhoto(), new
DateTime().getMillis(),
resultIntent, mCarInfo.getId());
}
private void showNotificationMessage(Context context, String title, String
message,
String imageUrl, long timeStamp, Intent intent, Long carId)
{
NotificationUtils notificationUtils = new NotificationUtils(context);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtils.showNotificationMessage(title, message, timeStamp,
intent, imageUrl,
carId);
}
}
这是我的通知实用程序类:
public class NotificationUtils
{
private Context mContext;
public NotificationUtils(){
}
public NotificationUtils(Context mContext){
this.mContext = mContext;
}
public static boolean isAppIsInForeground(Context context)
{
boolean isInForeground = false;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH){
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses){
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
for (String activeProcess : processInfo.pkgList){
if (activeProcess.equals(context.getPackageName())){
isInForeground = true;
}
}
}
}
}else{
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if (componentInfo.getPackageName().equals(context.getPackageName())){
isInForeground = true;
}
}
return isInForeground;
}
public void showNotificationMessage(final String title, final String message,
final long timeStamp, Intent intent, String imageUrl, Long carId){
// Check for empty push message
if (TextUtils.isEmpty(message))
return;
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
final PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext);
final Uri alarmSound = Uri.parse(
ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + mContext.getPackageName() + "/raw/notif");
showNotification(mBuilder, title, message, timeStamp, resultPendingIntent, alarmSound, carId);
}
private void showNotification(
NotificationCompat.Builder mBuilder, String title,
String message, long timeStamp, PendingIntent resultPendingIntent, Uri alarmSound,
Long carId){
Notification notification;
notification = mBuilder.setTicker(title)
.setAutoCancel(true)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(resultPendingIntent)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setWhen(timeStamp)
.setPriority(Notification.PRIORITY_HIGH)
.setSmallIcon(getNotificationIcon())
.setLargeIcon(
BitmapFactory.decodeResource(mContext.getResources(), R.drawable.logo))
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(
Context.NOTIFICATION_SERVICE);
notificationManager.notify(carId.intValue(), notification);
}
private int getNotificationIcon(){
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
return useWhiteIcon ? R.drawable.notif_silhouette : R.drawable.notif;
}
}
您是否在任何地方清除通知?这可能是设备问题,请尝试在其他设备中检查。