接收推送通知时打开其他数据

时间:2016-02-18 07:53:01

标签: android json

我创建了一个示例应用程序,并使用Pushwoosh我能够收到通知,点击后,它会打开应用程序,但是当我想发送其他数据时,它不显示。

  1. 我已添加以下代码。
  2. 我搜索过相关问题,但没有任何问题。这是我试图传递给app的数据。
  3. 
    
    {"employees":[{"firstName":"John", "lastName":"Doe"},{"firstName":"Anna", "lastName":"Smith"},{"firstName":"Peter", "lastName":"Jones"}]}
    
    
    

    这是MainActivity

    
    
    public class MainActivity extends AppCompatActivity
    {
        private static final String bTAG = "example";
    
        boolean broadcastPush = true;
    
        JSONAdapter jsonAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ArrayList<String> listB = new ArrayList<String>();
    
            String userNameText = "";
    
    
            JSONObject json = null;
            try
            {
                json = new JSONObject(userNameText);
            } catch (JSONException e)
            {
                e.printStackTrace();
            }
    
            JSONArray array = null;
            try
            {
                JSONObject userdataObject = json.getJSONObject("userdata");
    
                array = userdataObject.getJSONArray("employees");
            }
            catch (JSONException e)
            {
                e.printStackTrace();
            }
    
    
    
    
            //Set the adapter as the adapter of choice for our list
            jsonAdapter = new JSONAdapter(this, array);
    
            //here we register receivers for push notifications
            registerReceivers();
    
            final PushManager pushManager = PushManager.getInstance(this);
    
            //Now we start the push manager, this will count app open for Pushwoosh stats as well
            try
            {
                pushManager.onStartup(this);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
    
            //now we register for push notification
            pushManager.registerForPushNotifications();
    
            //then check launch notification
            String launchNotification = pushManager.getLaunchNotification();
            if(launchNotification != null)
            {
                Log.d(bTAG, "Launch notification received" + launchNotification);
            }
            else
            {
                Log.d(bTAG, "No notification received");
            }
    
            //Clear application icon badge number
            pushManager.setBadgeNumber(0);
        }
    
        /**
         * Called when the activity receives a new intent.
         */
        public void onNewIntent(Intent intent)
        {
            super.onNewIntent(intent);
            //have to check if we've got new intent as a part of push notification
            try {
                checkMessage(intent);
            } catch (JSONException e)
            {
                e.printStackTrace();
            }
        }
    
        //Registration receiver
        BroadcastReceiver mBroadcastReceiver = new BaseRegistrationReceiver()
        {
            @Override
            public void onRegisterActionReceive(Context context, Intent intent)
            {
                try {
                    checkMessage(intent);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };
    
        //Push message receiver
        private BroadcastReceiver mReceiver = new BasePushMessageReceiver()
        {
            @Override
            protected void onMessageReceive(Intent intent)
            {
                //JSON_DATA_KEY contains JSON payload of push notification.
                try
                {
                    doOnMessageReceive(intent.getExtras().getString(JSON_DATA_KEY));
                } catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }
        };
    
        //Registration of the receivers
        public void registerReceivers()
        {
            IntentFilter intentFilter = new IntentFilter(getPackageName() + ".action.PUSH_MESSAGE_RECEIVE");
    
            if (broadcastPush)
                registerReceiver(mReceiver, intentFilter, getPackageName() +".permission.C2D_MESSAGE", null);
    
            registerReceiver(mBroadcastReceiver, new IntentFilter(getPackageName() + "." + PushManager.REGISTER_BROAD_CAST_ACTION));
    
        }
    
        public void unregisterReceivers()
        {
            //Unregister receivers on pause
            try
            {
                unregisterReceiver(mReceiver);
            }
            catch (Exception e)
            {
                // pass.
            }
    
            try
            {
                unregisterReceiver(mBroadcastReceiver);
            }
            catch (Exception e)
            {
                //pass through
            }
        }
    
        @Override
        public void onResume()
        {
            super.onResume();
    
            //Re-register receivers on resume
            registerReceivers();
        }
    
        @Override
        public void onPause()
        {
            super.onPause();
    
            //Unregister receivers on pause
            unregisterReceivers();
        }
    
        /**
         * Will check PushWoosh extras in this intent, and fire actual method
         *
         * @param intent activity intent
         */
        private void checkMessage(Intent intent) throws JSONException {
            if (null != intent)
            {
                if (intent.hasExtra(PushManager.PUSH_RECEIVE_EVENT))
                {
                    doOnMessageReceive(intent.getExtras().getString(PushManager.PUSH_RECEIVE_EVENT));
                }
                else if (intent.hasExtra(PushManager.REGISTER_EVENT))
                {
                    doOnRegistered(intent.getExtras().getString(PushManager.REGISTER_EVENT));
                }
                else if (intent.hasExtra(PushManager.UNREGISTER_EVENT))
                {
                    doOnUnregistered(intent.getExtras().getString(PushManager.UNREGISTER_EVENT));
                }
                else if (intent.hasExtra(PushManager.REGISTER_ERROR_EVENT))
                {
                    doOnRegisteredError(intent.getExtras().getString(PushManager.REGISTER_ERROR_EVENT));
                }
                else if (intent.hasExtra(PushManager.UNREGISTER_ERROR_EVENT))
                {
                    doOnUnregisteredError(intent.getExtras().getString(PushManager.UNREGISTER_ERROR_EVENT));
                }
    
                resetIntentValues();
            }
        }
    
        public void doOnRegistered(String registrationId)
        {
            Log.d(bTAG, "registered: " + registrationId);
        }
    
        public void doOnRegisteredError(String errorId)
        {
            Log.d(bTAG, "registration error: " + errorId);
        }
    
        public void doOnUnregistered(String registrationId)
        {
            Log.d(bTAG, "unregistered: " + registrationId);
        }
    
        public void doOnUnregisteredError(String errorId)
        {
            Log.d(bTAG, "deregistration error: " + errorId);
        }
    
        public void doOnMessageReceive(String message) throws JSONException
        {
            Log.d(bTAG, "received push: " + message);
    
            JSONObject json  = new JSONObject(message);
            JSONArray array = json.getJSONArray("employees");
    
            for (int i=0; i<array.length(); i++)
            {
                JSONObject o = array.getJSONObject(i);
                System.out.println(o);
            }
        }
    
        /**
         * Will check main Activity intent and if it contains any PushWoosh data, will clear it
         */
        private void resetIntentValues()
        {
            Intent mainAppIntent = getIntent();
    
            if (mainAppIntent.hasExtra(PushManager.PUSH_RECEIVE_EVENT))
            {
                mainAppIntent.removeExtra(PushManager.PUSH_RECEIVE_EVENT);
            }
            else if (mainAppIntent.hasExtra(PushManager.REGISTER_EVENT))
            {
                mainAppIntent.removeExtra(PushManager.REGISTER_EVENT);
            }
            else if (mainAppIntent.hasExtra(PushManager.UNREGISTER_EVENT))
            {
                mainAppIntent.removeExtra(PushManager.UNREGISTER_EVENT);
            }
            else if (mainAppIntent.hasExtra(PushManager.REGISTER_ERROR_EVENT))
            {
                mainAppIntent.removeExtra(PushManager.REGISTER_ERROR_EVENT);
            }
            else if (mainAppIntent.hasExtra(PushManager.UNREGISTER_ERROR_EVENT))
            {
                mainAppIntent.removeExtra(PushManager.UNREGISTER_ERROR_EVENT);
            }
    
            setIntent(mainAppIntent);
        }
    
    }
    &#13;
    &#13;
    &#13;

    这是适配器

    &#13;
    &#13;
    class JSONAdapter extends BaseAdapter
    {
        private final Activity activity;
        private final JSONArray jsonArray;
        public JSONAdapter (Activity activity, JSONArray jsonArray)
        {
            assert activity != null;
            assert jsonArray != null;
    
            this.jsonArray = jsonArray;
            this.activity = activity;
        }
    
    
        @Override public int getCount() {
            if(null==jsonArray)
                return 0;
            else
                return jsonArray.length();
        }
    
        @Override public JSONObject getItem(int position) {
            if(null==jsonArray) return null;
            else
                return jsonArray.optJSONObject(position);
        }
    
        @Override public long getItemId(int position) {
            JSONObject jsonObject = getItem(position);
    
            return jsonObject.optLong("id");
        }
    
        @Override public View getView(int position, View convertView, ViewGroup parent)
        {
            if (convertView == null)
                convertView = activity.getLayoutInflater().inflate(R.layout.user_name_list, null);
    
            TextView text = (TextView)convertView.findViewById(R.id.text);
    
            JSONObject json_data = getItem(position);
    
            if(null!=json_data )
            {
                try {
                    text.setText(json_data.getString("firstName"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
    
            return convertView;
        }
    
    
    }
    &#13;
    &#13;
    &#13;

    请提供帮助,因为我不太确定如何获取我在Pushwoosh上添加的其他数据,以便在不进行硬编码的情况下在应用中显示。

0 个答案:

没有答案