Firebase onMessageReceived在后台app中没有调用它的替代方案

时间:2016-11-20 05:27:53

标签: android firebase-cloud-messaging

我需要你的帮助,我去谷歌并搜索了很多,当我的应用程序处于后台时我面临严重问题而我从后端发送通知然后通知即将到来但是当我制作时它没有加载数据我的应用程序从背景到forground,如果我触摸通知图标,那么我将获取数据。

我也转到此链接How to handle notification when app in background in Firebase

有人告诉你必须点击通知图标才能加载数据,这是FCM的另一种选择。

我试过了:

     @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.v(TAG, "From: " + remoteMessage.getFrom());
        // Check if message contains a data payload.
//        if (remoteMessage.getData().size() > 0) {
//            Log.v(TAG, "Message data payload: " + remoteMessage.getData().get("order"));
//
//        }
//        if (remoteMessage.getNotification() != null) {
//            Log.v(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
//
//        }
        sendNotification(remoteMessage.getData().get("order"));
    }
    private void sendNotification(String messageBody) {

        Log.e("inside sendNotification","sendNotification");
        JSONObject jobj_order= null;
        try {
            dbHelper=new DBHelper(this);
            jobj_order = new JSONObject(messageBody);
            dbHelper.insertOrder(jobj_order.getJSONObject("supplier").toString(),jobj_order.getJSONObject("request").toString(),jobj_order.getString("expiry"),jobj_order.getString("id"),jobj_order.getString("quantity"));
            Log.e("inside try block","inside try block");

            Intent broadcastIntent = new Intent();
            broadcastIntent.setAction(MainActivity.NOTIFY_ACTIVITY_ACTION );
            broadcastIntent.putExtra("reload", true);
            sendBroadcast(broadcastIntent);
        } catch (JSONException e) {
            e.printStackTrace();
        }




        MainActivity.isNotification=true;

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.splash_logo)
                .setContentTitle("Logizo")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

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

1 个答案:

答案 0 :(得分:1)

使用“数据消息”更容易,我认为您正在使用“通知”。数据消息始终加载类onMessageReceived。

以下是我的工作代码:

 @Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"));
}

private void sendNotification(String messageTitle,String messageBody) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0 /* request code */, intent,PendingIntent.FLAG_UPDATE_CURRENT);

    long[] pattern = {500,500,500,500,500};

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_name)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setVibrate(pattern)
            .setLights(Color.BLUE,1,1)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

如果这不起作用,请发布onReciveMessage代码实现。