背景中的FCM通知问题

时间:2017-10-14 08:02:49

标签: android push-notification firebase-cloud-messaging

我正在使用AWS集成FCM,但是当我的应用程序被杀并且通知到来时,我无法打开我想要的片段。我从FCM收到数据有效负载。当应用程序处于前台或后台时(即当用户最小化应用程序时),该应用程序正常工作。我花了一天时间用谷歌搜索了一切我仍然无法实现它,我认为在活动文件中我的新意图在应用程序被杀并且通知到来并且用户点击通知时不会被触发,它是打开默认活动但我希望当用户点击通知调用活动中的newIntent并更改我的目标片段。这是我的代码:

FCM RECIVER:

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
         if (remoteMessage.getData().size() > 0) {

            String  message = remoteMessage.getData().get("message");
             String  additionalData = remoteMessage.getData().get("additionalData");
             try {
                 JSONObject jsonObject=new JSONObject(additionalData);
                 orderId=Integer.parseInt(jsonObject.getString("orderId"));
             } catch (JSONException e) {
                 e.printStackTrace();
             }

             sendNotification(orderId, message);
         }

这是sendNotication方法:

private void sendNotification(Integer orderId,String message) {

        Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(),
                R.mipmap.ic_launcher);

        //AppMethod.setIntegerPreference(getApplicationContext(),NOTIFICATION_ID,)
        Intent intent = new Intent(this, MainActivity.class);
        Bundle mBundle = new Bundle();
        mBundle.putInt("order_id", orderId);
        intent.putExtras(mBundle);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setFlags(Intent.FLAG_FROM_BACKGROUND);
        intent.setAction(Long.toString(System.currentTimeMillis()));
        int dummyuniqueInt = new Random().nextInt(543254);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, dummyuniqueInt, intent,  PendingIntent.FLAG_UPDATE_CURRENT);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(icon)
                .setContentTitle(orderId+"")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
        //int notification_unique_id = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(dummyuniqueInt, notificationBuilder.build());

    }

这是用于接收意图的活动代码,当用户点击它打开我的默认片段时通知到来时意图不起作用,但我希望newIntent应该在这里触发并打开我的目标fragemnet,当app被杀死状态。 / p>

@Override
    protected void onNewIntent(Intent intent)
    {
        super.onNewIntent(intent);
        Toast.makeText(MainActivity.this,"Here",Toast.LENGTH_SHORT).show();
        bottomBar.setCurrentItem(0, false);
        getSupportFragmentManager().beginTransaction().replace(R.id.frame, new CheckInFragment(), CheckInFragment.Fragment_TAG).commit();
        if(intent.getAction()!=null && intent.getExtras()!=null){
            hideKeyboard(this);
                meetingId =intent.getExtras().getInt("order_id");
            }
        setIntent(intent);
    }

这是我的数据有效负载:

'GCM':{

'data': {
  'message': 'Receive my nottttitititiititititititiitititiitiitiitttttttttttt',
  'sound': 'default',
  'badge': 1,
   'priority': 'normal',
  'additionalData': {
    'orderId': 7,
  },
}

},

0 个答案:

没有答案