0

我正在尝试获取通知内容并将其自动存储到数据库中,一旦从 parse.com 发布通知,无论用户是否点击查看通知,数据都应保存在数据库中。

这是我迄今为止尝试过的,但仍然无法正常工作。它仅在用户单击通知时将内容保存到数据库中。

应用程序.java

public class Application extends android.app.Application{
  public Application() {
  }

  @SuppressWarnings("deprecation")
    public void onCreate() {
    super.onCreate();
      // Initialize the Parse SDK.
    Parse.initialize(this, "YwWVJ1IdukAXQx6WqksoRlA94k1OoJ6cHqdgsInHaTN", "fCh5pWNiSaHaFtuACufgs9va6wq31pte8nuaiCAG6Nb");

    // Specify an Activity to handle all pushes by default.
    PushService.setDefaultPushCallback(this, Notification.class);
  }        
} 

FireBackground.java

public class FireBackground extends ParsePushBroadcastReceiver {
    @Override
    public void onPushOpen(Context context, Intent intent) {
        Intent i = new Intent(context, Notification.class);
        i.putExtras(intent.getExtras());
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

通知.java

public class Notification extends ListActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ParseAnalytics.trackAppOpened(getIntent());
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        String message="";

         SQLiteDatabase db;

            db = openOrCreateDatabase(
                "notification.db"
                , SQLiteDatabase.CREATE_IF_NECESSARY
                , null
                );

          //CREATE TABLES AND INSERT MESSAGES INTO THE TABLES
            String CREATE_TABLE_NOTICES = "CREATE TABLE IF NOT EXISTS notices ("
                    + "ID INTEGER primary key AUTOINCREMENT,"
                    + "NOTIFICATIONS TEXT)";
            db.execSQL(CREATE_TABLE_NOTICES);

        if(extras !=null){
            String jsonData = extras.getString("com.parse.Data");
            try{
                JSONObject notification = new JSONObject(jsonData);
                 message = notification.getString("alert");

                    String sql =
                        "INSERT or replace INTO notices (NOTIFICATIONS) "
                        + "VALUES('" + message + "')";       
                    db.execSQL(sql);      

            }
            catch(JSONException e){
                Toast.makeText(getApplicationContext(), "Something went wrong with the notification", Toast.LENGTH_SHORT).show();
            }   
        }      
}

安卓清单文件

<application
 android:name="com.parse.starter.Application"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppBaseTheme" >
    <activity
        android:name="com.parse.starter.Notification"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name="com.parse.PushService" />

    <receiver android:name=".HomeActivity"
     android:exported="false" > 
        <intent-filter>
        <action android:name="com.parse.push.intent.RECEIVE" />
        <action android:name="com.parse.push.intent.DELETE" />
        <action android:name="com.parse.push.intent.OPEN" />
    </intent-filter>
    </receiver>

    <receiver android:name="com.parse.ParseBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.USER_PRESENT" />
        </intent-filter>
    </receiver>
    <receiver android:name="com.parse.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <!--
              IMPORTANT: If you change the package name of this sample app,
              change "com.parse.starter" in the lines
              below to match the new package name.
            -->
            <category android:name="com.parse.starter" />
        </intent-filter>
    </receiver>

如果有人指导我使用正确的方法,我将不胜感激。谢谢

4

2 回答 2

1

将通知数据保存到本地数据库的代码在 Activity 中运行。并且在打开通知时启动活动,因此名称为onPushOpen。因此,将保存到数据库的代码移动到FireBackground类似的方法中onPushRecieved(...)

于 2015-06-08T13:55:10.950 回答
1

您应该在 BroadcastReceiver 中覆盖 onPushReceive 方法,然后从意图中获取必要的数据。这是我检索 jsonData 的示例:

@Override
protected void onPushReceive(Context context, Intent intent) {
   Bundle extras = intent.getExtras();

   String jsonData = extras.getString("com.parse.Data");
   DBServiceFlow.save(intent);
   } catch (JSONException | ParseException e) {
       Log.e(TAG, "", e);
   }
}

然后你可以用后台线程实现你自己的服务来保存你的数据。例如,您可以只使用IntentService

public class DBService extends IntentService {

@Override
public void onHandleIntent(Intent intent) { 
    if (intent.getAction() == "com.tac.save_notification") {
        //parse your json here...
        DBHelper.save(...);
    }
}
}

PS:永远不要在主线程中使用 DB。(就像您在通知活动中显示的那样)

于 2015-06-08T13:58:00.807 回答