Firebase推送通知不会弹出,仅显示在通知栏中

时间:2018-07-24 06:22:26

标签: android firebase push-notification firebase-cloud-messaging

我在onMessageReceived()上为FCM服务编写了相同的代码。但是,当我从服务器触发通知时,如果应用程序在后台或前景中,则不会弹出窗口,并且通知会直接显示为通知栏中的图标。

但是当我尝试使用android 6时,我会弹出。但是当我在6以上签入时,我没有得到popup

    public class MyFirebaseMessaging  extends FirebaseMessagingService {
    String title="";
    String message,type,click_action;
    int no_of_messages;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        if(remoteMessage.getData().size()>0) {
            message = remoteMessage.getData().get("body");
            title = remoteMessage.getData().get("title");
            click_action = remoteMessage.getData().get("click_action");
            no_of_messages++;
            ShowNotification(message);
        }
        else
            Log.i("remoteMessage",message);
    }

    private void ShowNotification(String message) {
        Intent intent = new Intent(click_action);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("loading_flag","load");
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            @SuppressLint("WrongConstant")
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);

            // Configure the notification channel.
            notificationChannel.setDescription("Channel description");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationChannel.enableVibration(true);
            notificationManager.createNotificationChannel(notificationChannel);
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
        // if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        //   notificationBuilder.setSmallIcon(R.drawable.vadodaramunicipalcorporation);
        //  notificationBuilder.setColor(getResources().getColor(R.color.backgroundColor));
        //  } else {
        //     setSmallIcon(R.drawable.vadodaramunicipalcorporation)
        //  }
        notificationBuilder.setSmallIcon(R.mipmap.vadodaramunicipalcorporation)
            .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.vadodaramunicipalcorporation))
            .setContentTitle("You have new "+ title)
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setTicker("Hearty365")
            .setContentInfo("Info")
            .setContentText(message+" new Alerts")
            .setNumber(no_of_messages)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setStyle(new NotificationCompat.BigTextStyle())
            .setContentIntent(pendingIntent)
            .setPriority(Notification.PRIORITY_MAX);
        notificationManager.notify(0, notificationBuilder.build());
    }
}

通知

{
    "to":"token-key",
    "data" : {
         "body" : "4",
          "title" : "Alert",
          "click_action" : "Activity.DATA_CLICK"
    }
}

1 个答案:

答案 0 :(得分:0)

这是我不久前写的用于在Android Oreo中显示通知的功能,它可以正常工作,如果需要,可以尝试一下。

public void createNotification(String aTitle, String aMessage, Intent goToIntent) {
final int NOTIFY_ID = Constants.Notification_ID_1;
String name = "user_channel"; // They are hardcoded only for show it's just strings
String id = "user_channel_1"; // The user-visible name of the channel.
String description = "user_first_channel"; // The user-visible description of the channel.

PendingIntent pendingIntent;
NotificationCompat.Builder builder;

if (notifManager == null) {
    notifManager =
            (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    int importance = NotificationManager.IMPORTANCE_HIGH;
    NotificationChannel mChannel = notifManager.getNotificationChannel(id);
    if (mChannel == null) {
        mChannel = new NotificationChannel(id, name, importance);
        mChannel.setDescription(description);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        notifManager.createNotificationChannel(mChannel);
    }
    builder = new NotificationCompat.Builder(this, id);

    goToIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    pendingIntent = PendingIntent.getActivity(this, 0, goToIntent, 0);

    builder.setContentTitle(aTitle)  // required
            .setContentText(aMessage)  // required
            .setSmallIcon(android.R.drawable.ic_popup_reminder) // required
            .setDefaults(Notification.DEFAULT_ALL)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setTicker(aTitle)
            .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
} else {

    builder = new NotificationCompat.Builder(this);

    goToIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    pendingIntent = PendingIntent.getActivity(this, 0, goToIntent, 0);

    builder.setContentTitle(aTitle)    // required
            .setContentText(aMessage)  // required
            .setSmallIcon(android.R.drawable.ic_popup_reminder) // required
            .setDefaults(Notification.DEFAULT_ALL)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setTicker(aTitle)
            .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
            .setPriority(Notification.PRIORITY_HIGH);
}

Notification notification = builder.build();
notifManager.notify(NOTIFY_ID, notification);
}
相关问题