Android Firebase 通知(如果应用程序已打开)



我已经为我的 3° 应用程序 android 配置了 FCM,但在这种情况下,只有在应用程序关闭或后台时才会收到通知,当应用程序运行时,什么都没有显示。

这是我的.FirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "MyFirebaseMsgService";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());    
    }    
    private void sendNotification(String title, String body) {
        Random random = new Random();
        int m = random.nextInt(9999 - 1000) + 1000;
        Intent showFullQuoteIntent = new Intent(this, Home.class);
        showFullQuoteIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        // both of these approaches now work: FLAG_CANCEL, FLAG_UPDATE; the uniqueInt may be the real solution.
        //PendingIntent pendingIntent = PendingIntent.getActivity(this, uniqueInt, showFullQuoteIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        int uniqueInt = (int) (System.currentTimeMillis() & 0xfffffff);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, uniqueInt, showFullQuoteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Notification notification = new NotificationCompat.Builder(getApplicationContext(), "")
                .setSmallIcon(R.drawable.logo)
                .setContentTitle(title)
                .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.logo))
                .setColor(getResources().getColor(R.color.colorPrimary))
                .setAutoCancel(false)
                .setSound(notificationSound)
                .setContentIntent(pendingIntent)
                .setContentText(body)
                .build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(m, notification);
    }
}

呼入:

 <service
            android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>

你能帮我吗?FCM 已链接到我的 Firebase 帐户,我从 Firebase 网站发送通知...

FCM 日志:

D/FA: Logging event (FE): notification_foreground(_nf), Bundle[{firebase_event_origin(_o)=fcm, firebase_screen_class(_sc)=Home, firebase_screen_id(_si)=-8508869816157962301, message_device_time(_ndt)=0, message_name(_nmn)=test, message_time(_nmt)=1521054559, message_id(_nmid)=5215133478455582605}]

我使用了一些代码,例如:通知未显示在安卓 8 奥利奥谷歌指南:https://developer.android.com/training/notify-user/build-notification.html

但是,如果应用程序已打开,则未收到通知,但已登录控制台。

我已经用这段代码解决了:

private void sendNotification(String title, String body) {
        Random random = new Random();
        int m = random.nextInt(9999 - 1000) + 1000;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String CHANNEL_ID = "my_channesl_01";
            CharSequence name = "NEWS";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationManager mNotificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel mChannel = null;
            mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
            mNotificationManager.createNotificationChannel(mChannel);
            Intent showFullQuoteIntent = new Intent(this, Home.class);
            showFullQuoteIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            int uniqueInt = (int) (System.currentTimeMillis() & 0xfffffff);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, uniqueInt, showFullQuoteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Notification notification = new NotificationCompat.Builder(this.getApplicationContext(), "")
                    .setSmallIcon(R.drawable.logo)
                    .setContentTitle(title)
                    .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.logo))
                    .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
                    .setAutoCancel(false)
                    .setSound(notificationSound)
                    .setContentIntent(pendingIntent)
                    .setContentText(body)
                    .setChannelId(CHANNEL_ID)
                    .build();
            mNotificationManager.notify(m, notification);
        }
        else
        {
            Intent showFullQuoteIntent = new Intent(this, Home.class);
            showFullQuoteIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            int uniqueInt = (int) (System.currentTimeMillis() & 0xfffffff);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, uniqueInt, showFullQuoteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Notification notification = new NotificationCompat.Builder(getApplicationContext(), "")
                    .setSmallIcon(R.drawable.logo)
                    .setContentTitle(title)
                    .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.logo))
                    .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
                    .setAutoCancel(false)
                    .setSound(notificationSound)
                    .setContentIntent(pendingIntent)
                    .setContentText(body)
                    .build();
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify(m, notification);
        }
    }

相关内容