2016-01-24 41 views
0

我有一个关于足球/足球的应用程序,我使用API​​来获取关于比赛的信息,我需要一种方法来在此API中添加目标时进行实时通知它们具有JSON格式。通过JSON格式实时通知android

+0

你能发表一些代码吗? 你试过'NotificationCompat'吗? – gilgil28

+0

我没有为此编写任何代码,但我需要一种可以在此情况下使用的方法 – Shivo

+0

如果您在解决问题时遇到任何问题,将使用StackOverflow。至少先尝试 –

回答

0

,看看谷歌云消息(GCM)服务,它正是为此而设计的。如果使用gcm不可行,则需要设置重复警报,但不会像gcm那样精确。但我强烈建议去gcm。这里有一个供您参考的链接。 GCM dev docs

对于无法访问后端源代码的情况,另一种解决方法是开发一种中间层类型的Web服务,该服务不断轮询您的后端并使用gcm来提醒客户端。至少你不会浪费用户的系统资源。

0

这是你如何弹出一个通知:

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.logo_notification) 
       .setContentTitle(title) 
       .setContentText(body) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setVibrate(new long[] { 200, 200, 200}) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(id, notificationBuilder.build()); //id is the notification id, if you use the same id, the notification will override the previous 
+0

谢谢,但我的问题是如何在API请求中的数据发生更改时在特定时间弹出此通知 – Shivo

+0

您有听众吗?你怎么知道数据已经改变? – gilgil28

+0

数据根据比赛中的事件而变化,我是否应该每隔一分钟调用一次该请求以了解是否发生了任何更改或者有任何方法可以做到这一点? – Shivo

0

为了实现该功能,

  1. 使用服务经常(也许一次5分钟)查询有关比赛的信息。
  2. 如果您有任何更新,请向用户显示通知。

要显示通知:

PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0); 
Notification notification = new NotificationCompat.Builder(getApplicationContext()).setTicker("Ticker Text").setSmallIcon("Icon").setContentTitle("Title").setContentText("Content Text").setContentIntent(pi).setAutoCancel(true).build(); 
           NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
           notificationManager.notify(0, notification); 

欲了解更多信息,如果您有机会获得你的后端Web服务代码中使用此链接 here

0

你真正想要做的是有一个Receiver将运行并检查更新。对于实时更新,我会使用Socket。

这几乎取决于你的服务器。