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();
            }   
        }      
}

Android マニフェスト ファイル

<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 に答える