Android:通过通知推送数据

时间:2014-02-06 06:54:17

标签: android notifications push-notification push

我正在开发一个应用程序。该应用程序是基于客户端服务器。

申请的需要是:

  1. 我想在没有网络服务的情况下将数据从服务器推送到设备,就像推送通知一样。我想推送与通知相比更大的数据,数据可能是text,xml,json,.png,.jpg。

    我曾尝试过This Link

  2. 推送通知演示
  3. 每当有额外的数据添加到服务器时,只有那些数据应该从服务器推送到设备并发出通知。当用户点击通知数据从设备显示时,不想在点击Web服务器通知后获取数据。

  4. 请建议我,我正在申请中。

    因此,请建议我应该遵循哪些步骤来完成此任务。用你宝贵的知识指导我。

2 个答案:

答案 0 :(得分:1)

申请的需要是:

1. I want to push data from server to device without web service, As like push notification. I want to push data which are more in size as compare to the notification and the data may be text, xml, json, .png, .jpg any thing.

2. Whenever there is extra data added to the server, only that data should push from server to device with notification. When user click on the notification data gets display from device, don't want to fetch data after click on the notification with web server.

您可以以有效负载的形式推送数据,因为我们可以将消息从服​​务器发送到设备。但是我们可以发送的最大数据大小是4KB。

您只需将以下代码检查为:

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(context, MessageReceivedActivity.class);
    intent.putExtra("payload", payload);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
    notification.setLatestEventInfo(context, "Message", "New message received", pendingIntent);
    notificationManager.notify(0, notification);
}

愿这对你有所帮助。

答案 1 :(得分:0)

检查此link

注意:请确保使用GCM的“服务器密钥”而不是“android密钥”作为“API密钥”。