android如何存储推送的数据,Android推送通知:通过点击通知获取数据,存储和显示新活动...

我解决了以下问题:

>通过推送通知发送JSON数据.

答:能够在4kb的PHP JSON服务的帮助下从SERVER发送数据.

>将数据保存到SQLite数据库中.

A.当数据来自onMessage()中的推送通知时,在SQLite中保存数据

protected void onMessage(Context context, Intent intent) {

Log.i(TAG, "Received message");

String message = intent.getExtras().getString("price");

Log.d("OnMSG",message);

displayMessage(context, message);

DataBaseHelper dataBaseHelper = new DataBaseHelper(context);

dataBaseHelper.openDataBase();

dataBaseHelper.insertData(message);

dataBaseHelper.close();

// notifies user

generateNotification (context, message);

}

>点击推送通知打开新活动.

答:我使用onMessage()调用的生成通知函数中的待定意图来完成此操作.

private static void generateNotification(Context context, String message) {

int icon = R.drawable.ic_launcher;

long when = System.currentTimeMillis();

NotificationManager notificationManager = (NotificationManager)

context.getSystemService(Context.NOTIFICATION_SERVICE);

Notification notification = new Notification(icon, message, when);

String title = context.getString(R.string.app_name);

Intent notificationIntent = new Intent(context, MainActivity.class);

notificationIntent.putExtra("ms", message);

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

notification.setLatestEventInfo(context, title, message, intent);

notification.flags |= Notification.FLAG_AUTO_CANCEL;

notification.defaults |= Notification.DEFAULT_SOUND;

notification.defaults |= Notification.DEFAULT_VIBRATE;

notificationManager.notify(0, notification);

}

>显示来自新活动的推送通知的数据.

A.这实现了当新活动在点击通知时调用(从第3点代码开始)我从主要活动onCreate()中获取SQLite中的数据.

DataBaseHelper dataBaseHelper = new DataBaseHelper(this);

dataBaseHelper.openDataBase();

Cursor c = dataBaseHelper.getData();

String data = null;

if(c.getCount()>0){

if(c.moveToFirst()){

do{

data = c.getString(0);

} while(c.moveToNext());

}

} else {

data = "No Data";

}

>如果应用程序已关闭,那么点击通知后应用程序就会启动.A.此任务从第3点开始实现.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值