0

我在我的应用程序中收到的(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);
    }
}

这是我的 NotificationUtils 类:

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;
}

}

4

1 回答 1

0

您是否在任何地方清除通知?这可能是设备问题,尝试在其他设备中检查。

于 2017-05-22T11:21:42.340 回答