当应用程序在后台时,不会调用onMessageReceived

时间:2017-06-28 09:20:23

标签: android firebase notifications firebase-cloud-messaging

当应用程序正在运行且如果我点击它时通知是警报,则会通知那里(onMessageReceived被调用)

但是当应用程序处于后台时,如果我点击它就会发出通知,onMessageReceived未调用

如何解决?

PS。抱歉,我的英语不好。 我的代码

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    String fnotification="0";
    String post_id;
    String bage;
    String notification_id;
    String type;
    String message;

    private static final String TAG = "MyFirebaseMsgService";
    RemoteMessage remoteMessage;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Log.d(TAG, "onMessageReceived: ");
        RemoteMessage.Notification notification = remoteMessage.getNotification();
        Map<String, String> data = remoteMessage.getData();
        Log.e("FROM", remoteMessage.getFrom());
        post_id = data.get("post_id");
        bage = data.get("badge");
        type = data.get("type");
        message = data.get("message");
        notification_id = data.get("notification_id");

        Log.d("MyFirebaseMessagingServ","data=>"+data);
        Log.d("MyFirebaseMessagingServ","notification=>"+notification);
        sendNotification(notification, data);
        //push_token
    }

    @Override
    public void handleIntent(Intent intent) {
        super.handleIntent(intent);



    }

    private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        try {
            int int_badge_count = Integer.parseInt(bage);
            ShortcutBadger.with(getApplicationContext()).count(int_badge_count);
        } catch (Exception e) {

        }


        Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.icon_android);


        intent.putExtra("menuFragment", fnotification.toString());
        intent.putExtra("badges_count", bage);
        intent.putExtra("post_id",post_id);
        intent.putExtra("type",type);
        intent.putExtra("message",message);
        intent.putExtra("notification_id",notification_id);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle(notification.getTitle())
                .setContentText(notification.getBody())
                .setAutoCancel(true)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                //.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.win))
                .setContentIntent(pendingIntent)
               // .setContentInfo("Hello")
                .setLargeIcon(icon)
                //.setColor(Color.RED)
//                .setLights(Color.RED, 1000, 300)
                .setDefaults(Notification.DEFAULT_VIBRATE)
                .setPriority(Notification.PRIORITY_MAX)
                .setSmallIcon(getNotificationIcon());
//                .setSmallIcon(R.drawable.icon_android);

      /*  try {
            String picture_url = data.get("picture_url");
            if (picture_url != null && !"".equals(picture_url)) {
                URL url = new URL(picture_url);
                Bitmap bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                notificationBuilder.setStyle(
                        new NotificationCompat.BigPictureStyle().bigPicture(bigPicture).setSummaryText(notification.getBody())
                );
            }
        } catch (IOException e) {
            e.printStackTrace();
        }*/

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
        sendNoti(this);

    }

    private void sendNoti(Context context){
        Intent intent = new Intent("push");
        intent.putExtra("menuFragment",fnotification.toString());
        intent.putExtra("post_id", post_id);
        intent.putExtra("badges_count",bage);
        intent.putExtra("type",type);
        intent.putExtra("notification_id",notification_id);
        intent.putExtra("message",message);
        context.sendBroadcast(intent);
    }

    private int getNotificationIcon() {
        Log.d(TAG, "getNotificationIcon: ");
        boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
        return useWhiteIcon ? R.drawable.ic_notifications_white_24dp : R.drawable.icon_android;
//        return useWhiteIcon ? R.drawable.btn_menu_notification_1 : R.drawable.ic_launcher;

    }

0 个答案:

没有答案
相关问题