应用程序在前台时无法显示通知

时间:2017-05-29 09:10:20

标签: android firebase notifications android-notifications firebase-cloud-messaging

我正在尝试在我的Android应用程序中使用FCM获取推送通知,当从FCM控制台发送通知时,代码工作正常,它在前台和后台都可正常工作,但是当我通过FCM使用curl发送通知时,我正在获取数据这在logcat中是可见的,但仅当应用程序在后台时才显示通知,并且当应用程序在前台时不显示通知。每次从FCM推送数据时都会调用onMessageReceived,但当应用程序位于前台时,通知不可见。

 private void sendNotification(String body , Map<String,String> data) {

        int finalSecId = Integer.parseInt((String) data.get("sec_id"));
        int sec = Integer.parseInt((String) data.get("sec"));
        String extra1 = (String) data.get("extra1");
        String extra2 = (String) data.get("extra2");
        Intent intent = new Intent(this,HomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0/*Request code*/, intent, PendingIntent.FLAG_ONE_SHOT);
        //Set sound of notification
        Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


        NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.login_meter)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(body)
                .setAutoCancel(true)
                .setSound(notificationSound)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0 /*ID of notification*/, notifiBuilder.build());
    }

0 个答案:

没有答案