当应用处于后台

时间:2017-06-29 04:49:01

标签: android firebase notifications

当应用程序在后台时,通知会被提醒两次,当我点击时,只有第一个通知有数据而另一个是空的。

P.S。当应用程序在前台时不是问题! (通知是一个警报)

我的代码

    @Override
    public void handleIntent(Intent intent) {
        super.handleIntent(intent);
        Log.d(TAG, "handleIntent : " + intent.getExtras().toString());



        if (intent.getExtras() != null) {
            for (String key : intent.getExtras().keySet()) {
                Object value = intent.getExtras().get(key);
                Log.d(TAG, "handleIntent" + key + " "+ value);
            }
            Log.d("MyFirebaseMessagingServ","handleIntent=>"+ intent.getExtras() );
            Log.d("handleIntent","intent=>"+intent.getStringExtra("type"));
            Log.d("handleIntent","intent=>"+intent.getStringExtra("title"));
            sendDataNoti(intent);
        }else {
            Log.d(": ", "getExtras==null");


        }

    }

    private void sendDataNoti(Intent intentData) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

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

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

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
//        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle(intentData.getStringExtra("title"))
//                .setContentText(notification.getBody())
                .setContentText(intentData.getStringExtra("message"))
                .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);



        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() {
        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 个答案:

没有答案