我需要阅读 One Signal 的推送通知信息。取决于我需要在我的电子商务应用中更改产品的交付状态。
怎么读?
以下是有关如何在收到通知时运行自定义代码的 OneSignal 指南:
- 打开内容可用 (iOS) 或静默通知 (Android) 字段。这将导致您的应用程序在收到通知时在后台自动唤醒(即使它没有被点击)。您的自定义代码必须使用原生代码、Android 上的 Java 和 iOS 上的 Swift 或 Objective-C 编写。有关接收和处理事件的详细信息,请参阅 Apple 的适用于 iOS 的内容和我们的 Android 背景数据指南。
- 在您的应用程序中,我们提供了一个 API,您可以在发生上述情况时使用该 API 运行自定义代码。然后,您的自定义代码可以在设备上保存通知内容的副本,以便在下次启动应用程序时显示在活动源中。或者它可以在您的服务器上保存一份副本。
通知可以包含将传递给您的自定义代码的元数据(在 OneSignal API 中作为“数据”提供)。
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){}
}
}
添加
compile 'com.onesignal:OneSignal:[3.7.1, 3.99.99]'
在android下添加这段代码
manifestPlaceholders = [onesignal_app_id: "app_id Enter here",
onesignal_google_project_number: "REMOTE"]
这很简单
首先,您可以从 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;
}
}
您需要扩展 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();
}
}
}