在设备顶部栏上本地显示推送通知

时间:2014-01-15 20:26:05

标签: java android push-notification location

我想将我的Android应用注册到基于位置的推送通知。

我在这里得到了一些很棒的提示

这里有很好的建筑理念。

如果我理解正确,我没有必要使用GCM - 而是使用我自己的服务器。正确?

将我的位置推送到服务器并获得JSON响应后 - 如何在我的设备顶部栏本地显示它?

1 个答案:

答案 0 :(得分:0)

创建接收器以在收到消息时触发通知。 您可以使用以下代码创建通知:

    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());

   }