【发布时间】:2014-01-16 05:45:07
【问题描述】:
我想将我的 android 应用注册到基于位置的推送通知。
我在这里得到了一些很棒的提示
这里有不错的架构理念。
如果我理解正确,我没有必要使用 GCM - 而是使用我自己的服务器。对吧?
在我将我的位置推送到服务器并获得 JSON 响应后 - 如何在我的设备顶部栏本地显示它?
【问题讨论】:
标签: java android push-notification location
我想将我的 android 应用注册到基于位置的推送通知。
我在这里得到了一些很棒的提示
这里有不错的架构理念。
如果我理解正确,我没有必要使用 GCM - 而是使用我自己的服务器。对吧?
在我将我的位置推送到服务器并获得 JSON 响应后 - 如何在我的设备顶部栏本地显示它?
【问题讨论】:
标签: java android push-notification location
创建一个接收器以在接收到消息时触发通知。 您可以使用以下代码创建通知:
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
Context ctx;
private void sendNotification(Intent receivedIntent) {
mNotificationManager = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(ctx, ReceiveNotification.class);// this will open the class receiveNotification when the notification is clicked
intent.putExtra("author", receivedIntent.getStringExtra("author"));
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent , 0);
NotificationCompat.Builder noti = new NotificationCompat.Builder(ctx)
.setSmallIcon(R.drawable.ic_launcher) // if you want to include an icon
.setContentTitle("your app name")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("By: " + receivedIntent.getStringExtra("name")))
.setWhen(System.currentTimeMillis())
.setTicker("By: " + receivedIntent.getStringExtra("name"))
.setDefaults(Notification.DEFAULT_SOUND)
.setContentText("your message");
noti.setContentIntent(pendingIntent);
mNotificationManager.notify(1, noti.build());
}
【讨论】: