Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Firebase通知不存在';不显示在设备上_Android_Firebase_Firebase Notifications - Fatal编程技术网

Android Firebase通知不存在';不显示在设备上

Android Firebase通知不存在';不显示在设备上,android,firebase,firebase-notifications,Android,Firebase,Firebase Notifications,我在OnTokenRefresh()中暂停我的应用程序,复制令牌,将应用程序置于后台,然后使用Firebase控制台发送消息,消息显示令牌未注册 建议是,设备只有在后台运行时才会收到通知,而后台运行应用程序的行为似乎会注销令牌 为什么会发生这种情况?是的,在旧的Firebase云消息中是这样的,但您可以从Firebase获取数据,并使用wakeful广播接收器生成通知 public class FirebaseDataReceiver extends WakefulBroadcastReceiv

我在
OnTokenRefresh()
中暂停我的应用程序,复制令牌,将应用程序置于后台,然后使用Firebase控制台发送消息,消息显示令牌未注册

建议是,设备只有在后台运行时才会收到通知,而后台运行应用程序的行为似乎会注销令牌


为什么会发生这种情况?

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

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,它也会在后台和前台收到通知

lol,谢谢André。