2014-01-15 50 views
0

我想將我的android應用註冊到基於位置的推送通知。在設備頂欄上本地呈現推送通知

我這裏

和漂亮的建築理念在這裏得到了一些偉大的祕訣。

如果我理解正確,我沒有意思使用GCM - 而是使用我自己的服務器。對?

當我將我的位置推送到服務器並獲得JSON響應之後 - 我如何在設備頂部條上本地顯示它?

回答

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

    } 
+0

順便說一句,接收器將如何從服務器獲取信息?我應該註冊哪個活動\意圖? –