我需要阅读One Signal的推送通知信息。具体取决于我需要在我的电子商务应用中更改产品的投放状态。
如何阅读?
答案 0 :(得分:1)
以下是有关如何在收到通知时运行自定义代码的OneSignal指南:
- 启用内容可用(iOS)或无提示通知(Android) 领域。这将导致您的应用程序被自动唤醒 收到通知时在后台启动(即使 它没有被点击)。您的自定义代码必须使用本机代码编写, Android和Swift上的Java或iOS上的Objective-C。见Apple的 内容 - 适用于iOS和我们的Android背景数据指南 接收和处理事件的详细信息。
- 在您的应用中,我们 提供一个API,您可以使用它来运行上面的自定义代码 发生。然后,您的自定义代码可以保存通知的副本 设备上的内容,以便在活动Feed中显示 当应用程序下次启动时。或者它可以保存你的副本 服务器。
醇>
通知可以包含将传递给您的自定义代码的元数据(在OneSignal API中作为“数据”提供)。
答案 1 :(得分:1)
添加
compile 'com.onesignal:OneSignal:[3.7.1, 3.99.99]'
在android
下添加此代码manifestPlaceholders = [onesignal_app_id: "app_id Enter here",
onesignal_google_project_number: "REMOTE"]
答案 2 :(得分:0)
您需要扩展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();
}
}
}
答案 3 :(得分:0)
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){}
}
}
答案 4 :(得分:0)
这个很简单
首先,您可以从 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;
}
}