One Signal のプッシュ通知情報を読み取る必要があります。それに応じて、e コマース アプリで商品の配送ステータスを変更する必要があります。
読み方は?
One Signal のプッシュ通知情報を読み取る必要があります。それに応じて、e コマース アプリで商品の配送ステータスを変更する必要があります。
読み方は?
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){}
}
}
NotificationsReceiveHandler を拡張し、それを OneSignal インスタンスに設定する必要があります。
この例では、受信した通知をキャッチし、NotificationsType をチェックしています。
NotificationsType は、クライアント側での通知の役割をよりよく知るために、サーバー バックエンドで作成したオブジェクトです。
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();
}
}
}
これはとても簡単です
まず、アプリのローカル データベースである OneSignal.db からすべての通知を読み取ることができます。
以下でこのクラスを使用するだけで、OneSignal.dbからどのように読み取ることができますか
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;
}
}