2015-04-29 32 views
5

是否有反正在發送基於位置的推送通知android設備與使用第三方推送通知服務,如解析?我想發送推送通知給我的用戶,而不必煩惱收到與特定用戶無關的通知,因爲他們不在特定區域。另外,我可以根據時間間隔獲取用戶位置,但如果可能的話,我寧願以不同的方式進行操作。基於位置的推送通知對於Android

+0

看區域範圍設定。它可能會給你一個解決方案。例如根據入口/出口到地理圍欄的位置,讓您的申請註冊/取消註冊通知...? –

回答

9

是的,這是完全可能的,只要我正確地解釋你在問什麼。

要完成此操作,您需要將GCM推送通知發送給所有用戶(除非您有辦法,服務器端將其中的一些過濾掉)。然後在您的應用程序中,不是隻創建一個通知並將其傳遞給通知管理器,您應該首先使用LocationManager(或更新的LocationServices API)確定用戶是否位於適當的位置,然後放棄GCM如果不是,則通知。

你需要照顧的幾件事情,爲了做到這一點:

  • 您的AndroidManifest.xml將需要幾個權限更改,無論是對GCM變化,併爲Location訪問:

    <!-- Needed for processing notifications --> 
    <permission android:name="com.myappname.permission.C2D_MESSAGE" android:protectionLevel="signature" /> 
    <uses-permission android:name="com.myappname.permission.C2D_MESSAGE" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    
    <!-- Needed for Location --> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    
  • 您還需要建立一個通知接收器在清單的<application>部分:

    <receiver android:name="com.myappname.NotificationReceiver" android:permission="com.google.android.c2dm.permission.SEND"> 
        <intent-filter> 
         <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
         <category android:name="com.myappname" /> 
        </intent-filter> 
        <intent-filter> 
         <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
         <category android:name="com.myappname" /> 
        </intent-filter> 
    </receiver> 
    
  • 此外,你需要寫你NotificationReceiver Java類,並覆蓋onReceive功能:

    public class NotificationReceiver extends BroadcastReceiver { 
        public void onReceive(final Context context, final Intent intent) { 
    
         if ("com.google.android.c2dm.intent.REGISTRATION".equals(intent.getAction())) { 
    
          handleRegistration(context, intent); // you'll have to write this function 
    
         } else if ("com.google.android.c2dm.intent.RECEIVE".equals(intent.getAction())) { 
    
          // the handle message function will need to check the user's current location using the location API you choose, and then create the proper Notification if necessary. 
          handleMessage(context, intent); 
    
         } 
    } 
    
+0

謝謝!你知道一個可以解釋每一步的教程嗎?我是Android新手,我覺得這很有幫助 – user3471066

+1

這些是Google的GCM文檔:http://developer.android.com/google/gcm/gs.html。請注意,您必須擁有某種後端解決方案才能真正告知Google發送通知。 這些是確定用戶位置的文檔: http://developer.android.com/guide/topics/location/strategies.html 給他們一個通讀,嘗試一下,如果你完全卡住了你可以在這裏再問一次。 如果這回答你的問題,別忘了標記我的答案爲可接受的答案,並歡迎來到Stack Overflow。 –

+1

我已經添加了更多的答案。這不是一個簡單的編碼任務,但肯定是可行的。 –