Android的推送通知:获取数据,存储和显示新的活动上点击的通知 [英] Android push notification: Get data, store and display on new activity on click of notification

本文介绍了Android的推送通知:获取数据,存储和显示新的活动上点击的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发的是有推送通知功能的应用程序。我跟着以下链接的<一个href="http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/"相对=nofollow> Android的推送通知

I am developing an application which is having push notification functionality. I followed the following link as Android Push Notification

我试过,并成功地做在$ C $ generateNotification了C以下变化()。

I tried and successfully send URL and open the web page on click of notification by doing the following change in code of generateNotification().

/**
 * 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);

我能够将数据与推送通知从服务器帮助发送。 现在我要执行以下任务:

I am able to send the data with the help of push notification from the server. Now i want to perform following tasks:

  1. 通过推送通知发送JSON数据。

  1. Send JSON data via push notification.

将数据保存到SQLite数据库。

Save the data into SQLite database.

在点击的推送通知打开新的活动。

Open new activity on click of push notification.

显示的数据来自新的活动推送通知来了。

Display data coming from push notification of new activity.

如果该应用程序,点击通知应用程序开始后关闭左右。

If the application is closed so after click on notification the app get started.

所以,请指导我,我应该遵循什么步骤来执行上述任务。

So please guide me what steps should i follow to perform the above task.

推荐答案

我解决了这个问题:

  1. 通过推送通知发送JSON数据。 答:可以从服务器大小为4KB的PHP JSON服务的帮助下发送数据。

  1. Send JSON data via push notification. A. Able to send the data from SERVER with the help of PHP JSON service of size 4kb.

将数据保存到SQLite数据库。 A.保存的数据SQLite中,当数据来自推送通知中的onMessage()

Save the data into SQLite database. A. Saved the data in SQLite when data comes from push notification in onMessage()

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称为通知功能()。

  • Open new activity on click of push notification. A. I done this using pending intent in generate notification function called from 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);     
    }
    

  • 显示的数据来自新的活动推送通知到来。 答:这实现因为当新的活动调用上点击的通知(从上面第3点code)我的主要活动的onCreate()。获取数据的SQLite

  • Display data coming from push notification of new activity. A. This achieves as when new activity invokes on click of notification (from above point 3 code) I get data from SQLite in main activity onCreate().

    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";
    }
    

  • 如果该应用程序,点击通知应用程序开始后关闭左右。 答:这个任务是从点3号来实现的。

  • If the application is closed so after click on notification the app get started. A. This task is achieved from point no 3.

    这篇关于Android的推送通知:获取数据,存储和显示新的活动上点击的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆