我正在开发一个具有推送通知功能的应用程序。我点击了以下链接 Android Push Notification
我尝试通过对 generateNotification() 代码进行以下更改,成功发送 URL 并在点击通知时打开网页。
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Message received", System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
//adding LED lights to notification
notification.defaults |= Notification.DEFAULT_LIGHTS;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(message));
//startActivity(browserIntent);
//PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
notification.setLatestEventInfo(context, "Message", "New message received", pendingIntent);
notificationManager.notify(0, notification);
我可以借助服务器的推送通知发送数据。 现在我想要执行以下任务:
通过推送通知发送 JSON 数据。
将数据保存到SQLite数据库中。
点击推送通知打开新 Activity 。
显示来自新 Activity 推送通知的数据。
如果应用程序已关闭,那么在点击通知后应用程序就会启动。
所以请指导我应该遵循哪些步骤来执行上述任务。
最佳答案
我解决的问题是:
通过推送通知发送 JSON 数据。 A. 能够借助大小为 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); }
点击推送通知打开新 Activity 。 答:我使用挂起的 Intent 在生成从 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); }
显示来自新 Activity 推送通知的数据。 A. 这是在点击通知时调用新 Activity 时实现的(从上面的第 3 点代码)我在主 Activity 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 点开始完成的。
关于Android 推送通知 : Get data, 在点击通知时存储并显示在新 Activity 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21871454/