我正在尝试获取通知内容并自动将其存储到数据库中,一旦从parse.com发布通知,数据应保存在数据库中,无论用户是否单击以查看通知。
这是我到目前为止所尝试的但仍然无效。它仅在用户单击通知时将内容保存到数据库中。
Application.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);
}
}
Notification.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>
如果有人指示我使用正确的方法,我将不胜感激。感谢
答案 0 :(得分:1)
您将通知数据保存到本地数据库的代码在活动中运行。并且在打开通知时启动活动,因此名称为onPushOpen
。因此,请使用FireBackground
onPushRecieved(...)
答案 1 :(得分:1)
您应该覆盖BroadcastReceiver中的onPushReceive方法,而不是获取必要的数据 从意图。 这是我的retreiving jsonData的例子:
var path = svg.datum(data).selectAll("path")
.data(partition.nodes)
.enter().append("path")
.attr("display", function(d) { return d.depth ? null : "none"; }) // hide inner ring
.attr("d", arc)
.attr('id',function(d) {
return d.name+"-"+d.value;
})
.style("stroke", "#fff")
.style("fill", function(d) {
if (d.depth > 0) {
return color(d.name);
}
}) .each(stash)
.on("mouseover", mouseover)
//.on("mouseenter",mouseover)
.on("mouseleave", mouseleave)
然后,您可以使用后台线程实现自己的服务以保存数据。例如,您可以使用IntentService
@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);
}
}
PS:从不在主线程中使用DB。 (就像你在通知活动中显示的那样)