Firebase通知未显示在设备

时间:2018-04-24 14:55:58

标签: android firebase firebase-notifications

我在OnTokenRefresh()暂停我的应用,复制令牌,将应用置于后台,然后使用Firebase控制台发送消息,并说明令牌未注册。

建议是设备只会在后台接收通知,但是应用程序的后台操作似乎取消注册令牌。

为什么会这样?

1 个答案:

答案 0 :(得分:1)

是的,在旧的Firebase云消息传递中就是这样但您可以从firebase获取数据并使用唤醒广播接收器生成通知

public class FirebaseDataReceiver extends WakefulBroadcastReceiver {
    private Utils utils = new Utils();

    @Override
    public void onReceive(Context context, Intent intent) {
        String mediaType = intent.getExtras().getString("mediaType");
//        Log.e("BroadcastReceiver::", "BroadcastReceiver");
        if (mediaType != null) {
            String message = intent.getExtras().getString("message");
            String imageUri = intent.getExtras().getString("image");
            String newsId = intent.getExtras().getString("newsId");
            Intent floating = new Intent(context, ChatHeadService.class);
            floating.putExtra("imageUri", imageUri);
            floating.putExtra("newsId", newsId);
            floating.putExtra("message", message);
            floating.putExtra("mediaType", mediaType);
            Bitmap bitmap = getBitmapfromUrl(imageUri, context);
            sendNotification(context, message, bitmap, imageUri, newsId, mediaType, null);
            if (Build.VERSION.SDK_INT >= 23) {
                if (!Settings.canDrawOverlays(context)) {
                    utils.cToast("Please enable Overlay permission for My News", context);
                    Intent settingIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                            Uri.parse("package:" + context.getPackageName()));
                    settingIntent.addFlags(FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(settingIntent);
                } else if (utils.getPrefernces(context, "dnd") == null)
                    context.startService(floating);
                else if (utils.getPrefernces(context, "dnd").equalsIgnoreCase("true"))
                    context.startService(floating);
            } else if (utils.getPrefernces(context, "dnd") == null)
                context.startService(floating);
            else if (utils.getPrefernces(context, "dnd").equalsIgnoreCase("true"))
                context.startService(floating);
        }
    }

    public void sendNotification(Context context, String messageBody, Bitmap image, String imageUri, String newsId, String mediaType, String TrueOrFalse) {
        Intent intent = null;

        switch (mediaType) {

            case "print":

                intent = new Intent(context, FullPrintMedia.class);
//                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("nid", newsId);
                intent.putExtra("mediaType", mediaType);
                break;

            case "electronic":

                intent = new Intent(context, FullElectronicMedia.class);
//                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("nid", newsId);
                intent.putExtra("mediaType", mediaType);
                break;

            case "live":

                intent = new Intent(context, Live.class);
//                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                break;

            case "update":

                //Open the app page in Google Play store:
                intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=live.mynews.app"));
                intent.addFlags(FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                break;

            case "add_friend":

                intent = new Intent(context, Collapsinglayout.class);
                intent.putExtra("userId", newsId);
                intent.putExtra("userName", messageBody.substring(0, messageBody.indexOf(' ')));
                intent.putExtra("userImage", imageUri);
                intent.putExtra("isFriend", 3);
                break;

            case "accept_friend":

                intent = new Intent(context, Collapsinglayout.class);
                intent.putExtra("userId", newsId);
                intent.putExtra("userName", messageBody.substring(0, messageBody.indexOf(' ')));
                intent.putExtra("userImage", imageUri);
                intent.putExtra("isFriend", 0);
                break;

            default:

                intent = new Intent(context, FullPrintMedia.class);
//                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("nid", newsId);
                intent.putExtra("mediaType", mediaType);
                break;
        }

//        intent.putExtra("AnotherActivity", TrueOrFalse);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 /* Request code */, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                .setLargeIcon(image)/*Notification icon image*/
                .setSmallIcon(new MyUtils().getNotificationIcon())
                .setContentTitle(messageBody)
                .setColor(ContextCompat.getColor(context, R.color.orange))
                .setStyle(new NotificationCompat.BigPictureStyle()
                        .bigPicture(image))/*Notification with Image*/
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setPriority(Notification.PRIORITY_MAX)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

    public Bitmap getBitmapfromUrl(String imageUrl, Context context) {
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            return BitmapFactory.decodeStream(input);
//            return bitmap;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.noimageavailable);
            e.printStackTrace();
            return icon;
        }
    }
}

我有同样的问题并编写了这个逻辑,帮助我解决了这个问题。

但是如果您更新SDK,它将在后台和前台接收通知