如果应用程序已打开,则会安装Android Firebase通知

时间:2018-03-14 18:41:26

标签: java android firebase firebase-cloud-messaging

我为我的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}]

我使用了一些代码: Notification not showing in Android 8 Oreo 来自Google的指南:https://developer.android.com/training/notify-user/build-notification.html

但是如果应用程序打开了通知未收到但登录控制台..

1 个答案:

答案 0 :(得分:1)

我用这段代码解决了:

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