0

I am trying to get notification content and store it into the database automatically, as soon as the notification is released from parse.com the data should be saved in the database whether the user clicks to view the notification or not.

This is what i have tried so far but still not working. It only save the content into a database when a user clicks on the notification.

Application.java

public class Application extends android.app.Application{
  public Application() {
  }

  @SuppressWarnings("deprecation")
    public void onCreate() {
    super.onCreate();
      // Initialize the Parse SDK.
    Parse.initialize(this, "YwWVJ1IdukAXQx6WqksoRlA94k1OoJ6cHqdgsInHaTN", "fCh5pWNiSaHaFtuACufgs9va6wq31pte8nuaiCAG6Nb");

    // Specify an Activity to handle all pushes by default.
    PushService.setDefaultPushCallback(this, Notification.class);
  }        
} 

FireBackground.java

public class FireBackground extends ParsePushBroadcastReceiver {
    @Override
    public void onPushOpen(Context context, Intent intent) {
        Intent i = new Intent(context, Notification.class);
        i.putExtras(intent.getExtras());
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

Notification.java

public class Notification extends ListActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ParseAnalytics.trackAppOpened(getIntent());
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        String message="";

         SQLiteDatabase db;

            db = openOrCreateDatabase(
                "notification.db"
                , SQLiteDatabase.CREATE_IF_NECESSARY
                , null
                );

          //CREATE TABLES AND INSERT MESSAGES INTO THE TABLES
            String CREATE_TABLE_NOTICES = "CREATE TABLE IF NOT EXISTS notices ("
                    + "ID INTEGER primary key AUTOINCREMENT,"
                    + "NOTIFICATIONS TEXT)";
            db.execSQL(CREATE_TABLE_NOTICES);

        if(extras !=null){
            String jsonData = extras.getString("com.parse.Data");
            try{
                JSONObject notification = new JSONObject(jsonData);
                 message = notification.getString("alert");

                    String sql =
                        "INSERT or replace INTO notices (NOTIFICATIONS) "
                        + "VALUES('" + message + "')";       
                    db.execSQL(sql);      

            }
            catch(JSONException e){
                Toast.makeText(getApplicationContext(), "Something went wrong with the notification", Toast.LENGTH_SHORT).show();
            }   
        }      
}

android manifest file

<application
 android:name="com.parse.starter.Application"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppBaseTheme" >
    <activity
        android:name="com.parse.starter.Notification"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name="com.parse.PushService" />

    <receiver android:name=".HomeActivity"
     android:exported="false" > 
        <intent-filter>
        <action android:name="com.parse.push.intent.RECEIVE" />
        <action android:name="com.parse.push.intent.DELETE" />
        <action android:name="com.parse.push.intent.OPEN" />
    </intent-filter>
    </receiver>

    <receiver android:name="com.parse.ParseBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.USER_PRESENT" />
        </intent-filter>
    </receiver>
    <receiver android:name="com.parse.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <!--
              IMPORTANT: If you change the package name of this sample app,
              change "com.parse.starter" in the lines
              below to match the new package name.
            -->
            <category android:name="com.parse.starter" />
        </intent-filter>
    </receiver>

I would be grateful if anyone would direct me to use the correct method. Thanks

Pankaj
  • 7,908
  • 6
  • 42
  • 65
George
  • 1,086
  • 14
  • 48
  • "I am trying to get notification content and store it into the database automatically" and then you say it has been already solved "It only save the content into a database when a user clicks on the notification". What is the clear problem and question? – deadfish Jun 08 '15 at 13:02
  • I think you are not getting me ryt. The above codes is able to save notification content into the database only when the user clicks on the notification icon from his phone interface. However if the user accesses the app without clicking on the notification icon from his fone, the content wld not be saved in the local datbase. – George Jun 08 '15 at 13:07
  • @deadfish please am I clear with my problem now? – George Jun 08 '15 at 13:13

2 Answers2

1

Your code for saving the notification data to your local database runs in the Activity. And the activity is started when the notification is opened, hence the nameonPushOpen. Hence, move the code that saves to the database to FireBackground in a method like onPushRecieved(...)

Aashir
  • 2,621
  • 4
  • 21
  • 37
1

You should override onPushReceive method in your BroadcastReceiver and than get necessary data from intent. Here is my example with retreiving jsonData:

@Override
protected void onPushReceive(Context context, Intent intent) {
   Bundle extras = intent.getExtras();

   String jsonData = extras.getString("com.parse.Data");
   DBServiceFlow.save(intent);
   } catch (JSONException | ParseException e) {
       Log.e(TAG, "", e);
   }
}

Then you could implement your own Service with background thread to save your data. For example you could just use IntentService

public class DBService extends IntentService {

@Override
public void onHandleIntent(Intent intent) { 
    if (intent.getAction() == "com.tac.save_notification") {
        //parse your json here...
        DBHelper.save(...);
    }
}
}

PS: never work with DB in main thread. (like you show in your Notification Activity)

Yevgen Kulik
  • 5,713
  • 2
  • 22
  • 44
TAC
  • 341
  • 1
  • 5
  • Hello, please i dont really understand what you have done. Can you explain a bit further. Thanks – George Jun 08 '15 at 14:15
  • 1
    DBHelper - static class BD layer DAO. DBService - intent service that is reciver for DB iteractions requests. will handle requests in background thread. DBServiceFlow - static class that will help you compose Intents and call service. – TAC Jun 08 '15 at 14:22
  • Should the data be saved through the DBService class? where are you storing the data into the database? – George Jun 08 '15 at 14:25
  • 1
    you could parse your data in onHandleIntent, and then store it in save method of your static class. So you will extract your method to DAO(Data Access Object). – Yevgen Kulik Jun 08 '15 at 14:32
  • Please could you use my code above to give an illustration for me to understand better. Thanks – George Jun 08 '15 at 14:35