Android push notification: Get data, store and display on new activity on click of notification

前端 未结 3 1619
孤街浪徒
孤街浪徒 2020-12-13 05:14

I am developing an application which is having push notification functionality. I followed the following link as Android Push Notification

I tried and successfully s

3条回答
  •  青春惊慌失措
    2020-12-13 06:06

    I solved the issues as:

    1. Send JSON data via push notification. A. Able to send the data from SERVER with the help of PHP JSON service of size 4kb.

    2. Save the data into SQLite database. A. Saved the data in SQLite when data comes from push notification in onMessage()

      protected void onMessage(Context context, Intent intent) {
          Log.i(TAG, "Received message");
          String message = intent.getExtras().getString("price");
          Log.d("OnMSG",message);
      
          displayMessage(context, message);
      
          DataBaseHelper dataBaseHelper = new DataBaseHelper(context);
          dataBaseHelper.openDataBase();
          dataBaseHelper.insertData(message);
          dataBaseHelper.close();
      
          // notifies user
          generateNotification (context, message);
      }
      
    3. Open new activity on click of push notification. A. I done this using pending intent in generate notification function called from onMessage().

      private static void generateNotification(Context context, String message) {
          int icon = R.drawable.ic_launcher;
          long when = System.currentTimeMillis();
          NotificationManager notificationManager = (NotificationManager)
              context.getSystemService(Context.NOTIFICATION_SERVICE);
          Notification notification = new Notification(icon, message, when);
      
          String title = context.getString(R.string.app_name);
      
          Intent notificationIntent = new Intent(context, MainActivity.class);
          notificationIntent.putExtra("ms", message);
          notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
          PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
          notification.setLatestEventInfo(context, title, message, intent);
          notification.flags |= Notification.FLAG_AUTO_CANCEL;
      
          notification.defaults |= Notification.DEFAULT_SOUND;
      
          notification.defaults |= Notification.DEFAULT_VIBRATE;
          notificationManager.notify(0, notification);     
      }
      
    4. Display data coming from push notification of new activity. A. This achieves as when new activity invokes on click of notification (from above point 3 code) I get data from SQLite in main activity onCreate().

      DataBaseHelper dataBaseHelper = new DataBaseHelper(this);
      dataBaseHelper.openDataBase();
      Cursor c = dataBaseHelper.getData();
      String data = null;
      if(c.getCount()>0){
          if(c.moveToFirst()){
              do{
              data = c.getString(0);
          } while(c.moveToNext());
          }
      } else {
          data = "No Data";
      }
      
    5. If the application is closed so after click on notification the app get started. A. This task is achieved from point no 3.

提交回复
热议问题