【问题标题】:How to read One Signal Push Notification Message in android?如何在 android 中读取 One Signal Push Notification Message?
【发布时间】:2016-08-02 18:30:00
【问题描述】:

我需要阅读 One Signal 的推送通知信息。取决于我需要在我的电子商务应用中更改产品的交付状态。

怎么读?

【问题讨论】:

  • 您正在使用 GCM 推送通知还是使用了任何第三方...
  • 一个信号。这是第三方通知。
  • 你处理过这个吗?

标签: android push-notification onesignal


【解决方案1】:

这里是关于如何在收到通知时运行自定义代码的 OneSignal 指南:

  1. 开启内容可用 (iOS) 或静默通知 (Android) 字段。这将导致您的应用程序被自动唤醒 每当收到通知时都会在后台启动(即使 它没有被点击)。您的自定义代码必须使用本机代码编写, Android 上的 Java 和 iOS 上的 Swift 或 Objective-C。见苹果的 内容适用于 iOS 和我们的 Android 背景数据指南 有关接收和处理事件的详细信息。
  2. 在您的应用程序中,我们 提供一个 API,您可以在上述情况下使用该 API 来运行自定义代码 发生。然后,您的自定义代码可以保存通知的副本 设备上的内容,以便在活动源中显示 下次启动应用程序时。或者它可以将它的副本保存在您的 服务器。

通知可以包含将传递给您的自定义代码的元数据(在 OneSignal API 中作为“数据”提供)。

【讨论】:

    【解决方案2】:

    您需要扩展 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】:

      添加

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

      在android下添加这段代码

      manifestPlaceholders = [onesignal_app_id: "app_id Enter here",
                               onesignal_google_project_number: "REMOTE"]
      

      【讨论】:

        【解决方案4】:
        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){} 
            }
        }
        

        【讨论】:

          【解决方案5】:

          这很简单

          首先您可以从 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;
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-05-15
            • 1970-01-01
            • 2016-07-22
            • 2018-10-21
            • 2014-07-25
            • 1970-01-01
            • 2021-07-04
            • 2018-12-16
            相关资源
            最近更新 更多