7

I need to read One Signal's Push Notification information.Depends on that I need to change the delivery status of a product in my eCommerce app.

How to read it?

Parama Sudha
  • 2,583
  • 3
  • 29
  • 48

6 Answers6

1

Here is the OneSignal guide on how to run custom code when a notification is received:

  1. Turn on the content-available (iOS) or silent-notification (Android) fields. This will cause your application to be automatically woken up in the background whenever a notification is received (even if it's not clicked). Your custom code must be write with native code, Java on Android and Swift or Objective-C on iOS. See Apple's content-available for iOS and our Android Background Data guides for details on receiving and processing the event.
  2. In your app, we provide an API that you can use to run custom code when the above occurs. Your custom code can then save a copy of the notification content on the device in order to be displayed in an activity feed when the app is next launched. Or it could save a copy of it on your servers.

Notifications can contain metadata (supplied as "data" in the OneSignal API) that will be passed to your custom code.

Gdeglin
  • 12,432
  • 5
  • 49
  • 65
1

Add

compile 'com.onesignal:OneSignal:[3.7.1, 3.99.99]'

add this code under android

manifestPlaceholders = [onesignal_app_id: "app_id Enter here",
                         onesignal_google_project_number: "REMOTE"]
BEingprabhU
  • 1,618
  • 2
  • 21
  • 28
1
class ExampleNotificationReceivedHandler implements OneSignal.NotificationReceivedHandler {
    @Override
    public void notificationReceived(OSNotification notification) {

       try {
        String type = data.optString("type","");
        if (type.equals("your_silent_type")) { // your condition
            notification.displayType = OSNotification.DisplayType.None;
            notification.isAppInFocus = false;

            // if notification already shown then just remove it, from notification tray
            String ns = Context.NOTIFICATION_SERVICE;
            NotificationManager nMgr = (NotificationManager) getSystemService(ns);
            nMgr.cancel(notification.androidNotificationId);

        }
       } catch(Exception e){} 
    }
}
Asad Ali Choudhry
  • 4,985
  • 4
  • 31
  • 36
0

you need to extends NotificationsReceiveHandler and set it into your OneSignal instance.

In this example I'm catching the notification as it gets received and I'm checking the NotificationsType.

NotificationsType is an object I've created in the server backend in order to know better about the notification roles in the client side.

public class CustomNotificationReceivedHandler implements OneSignal.NotificationReceivedHandler {

@Override
public void notificationReceived(OSNotification notification) {

    try {
        NotificationsType type = NotificationsType.fromValue(notification.payload.additionalData.optInt("type"));
        if (type.getValue() == NotificationsType.NEW_MESSAGE.getValue()) {
            notification.displayType = OSNotification.DisplayType.None;

            final Message message = new Gson().fromJson(notification.payload.additionalData.getString("payload"), Message.class);

            EventBus.getDefault().post(new NewMessageReceived(message));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}
Nativ
  • 3,092
  • 6
  • 38
  • 69
0

this is very simple

firstly you can read all notification from OneSignal.db it is a local database in your app

now how can you read from OneSignal.db just use this class in below

public class DataBaseHelper extends SQLiteOpenHelper {

    private Context mycontext;
    private static String DB_NAME = "OneSignal.db";
    private static String DB_PATH = "/data/data/" + BuildConfig.APPLICATION_ID + "/databases/";
    public SQLiteDatabase myDataBase;

    public DataBaseHelper(Context context) throws IOException {
        super(context, DB_NAME, null, 8);
        this.mycontext = context;
        boolean dbexist = checkdatabase();
        if (dbexist) {
            System.out.println("Database exists");
            opendatabase();
        } else {
            System.out.println("Database doesn't exist");
            createdatabase();
        }
    }

    public  void addmessgetodatabse(String message)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("feild2", message);

        db.insert("data", null, values);
    }
    public void createdatabase() throws IOException {
        boolean dbexist = checkdatabase();
        if (dbexist) {
            System.out.println(" Database exists.");
        } else {
            this.getReadableDatabase();
            this.close();
            try {
                copydatabase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        }
    }

    private boolean checkdatabase() {

        boolean checkdb = false;
        try {
            String myPath = DB_PATH + DB_NAME;
            File dbfile = new File(myPath);
            checkdb = dbfile.exists();
        } catch (SQLiteException e) {
            System.out.println("Database doesn't exist");
        }
        return checkdb;
    }

    private void copydatabase() throws IOException {
        //Open your local db as the input stream
        InputStream myinput = mycontext.getAssets().open(DB_NAME);

        // Path to the just created empty db
        String outfilename = DB_PATH + DB_NAME;

        //Open the empty db as the output stream
        OutputStream myoutput = new FileOutputStream(outfilename);

        // transfer byte to inputfile to outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myinput.read(buffer)) > 0) {
            myoutput.write(buffer, 0, length);
        }

        //Close the streams
        myoutput.flush();
        myoutput.close();
        myinput.close();
    }
    public void opendatabase() throws SQLException {
        //Open the database
        String mypath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(MyGolas.CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        if(i1==2)
        {
            sqLiteDatabase.execSQL(MyGolas.CREATE_TABLE);
        }
    }

    public void deleteNote(MyGolas note) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(MyGolas.TABLE_NAME, MyGolas.COLUMN_ID + " = ?",
                new String[]{String.valueOf(note.getId())});
        db.close();
    }
    public List<MyGolas> getAllGOLES() {
        List<MyGolas> notes = new ArrayList<>();

        // Select All Query
        String selectQuery = "SELECT  * FROM " + MyGolas.TABLE_NAME + " ORDER BY " +
                MyGolas.COLUMN_TIMESTAMP + " DESC";

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                MyGolas note = new MyGolas();
                note.setId(cursor.getInt(cursor.getColumnIndex(MyGolas.COLUMN_ID)));
                note.setTitle(cursor.getString(cursor.getColumnIndex(MyGolas.COLUMN_title)));
                note.setNote(cursor.getString(cursor.getColumnIndex(MyGolas.COLUMN_NOTE)));
                note.setTimestamp(cursor.getString(cursor.getColumnIndex(MyGolas.COLUMN_TIMESTAMP)));

                notes.add(note);
            } while (cursor.moveToNext());
        }

        db.close();

        return notes;
    }
    public ArrayList<String> getmessage() {
        ArrayList<String> contactList = new ArrayList<String>();
        String selectQuery;
            selectQuery = "SELECT * FROM notification"  ;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {
                contactList.add(cursor.getString(9));
            } while (cursor.moveToNext());
        }
        return contactList;
    }

    public String getgolas() {
        String result ="";
        String selectQuery;
        selectQuery = "SELECT  title FROM mygolse"  ;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {
             result = cursor.getString(0);
            } while (cursor.moveToNext());
        }
        return result;
    }
}
SSK
  • 3,444
  • 6
  • 32
  • 59
hamdy
  • 1
  • 2
0

how to call in android manifest by using metadata

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 19 '22 at 10:06
  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/late-answers/31325739) – Hooman Mar 23 '22 at 22:02