2012-10-12 58 views
0

我一直在閱讀有關使用TCP/IP創建推送通知後,它似乎像介紹的步驟有可能是有些過時」 http://tokudu.com/2010/how-to-implement-push-notifications-for-android/的Android - 創建推送通知有事服務器

真的,我只需要警惕「xyz動作發生在abc應用程序上」的用戶

我之前被建議使用UrbanAirship,但我不確定這是如何適合的東西,我猜這是因爲我需要從安全連接發送警報

但是,我該如何發送警報?我是否只需要phone-id或什麼 像那樣?什麼將是一個很好的教程呢?

謝謝!

回答

1

你可以使用服務器部分的代碼:

package yourpackage.android.gcm.server; 

import com.google.android.gcm.server.Message; 
import com.google.android.gcm.server.MulticastResult; 
import com.google.android.gcm.server.Sender; 

import java.util.ArrayList; 

class Notify { 
    public static void main(String args[]) { 

     try { 
             // this API key  
      Sender sender = new Sender("AIzaSyCn3N2OIm-EDtiGwTyQfSIB8NRvDtIOx30"); 

      ArrayList<String> devicesList = new ArrayList<String>(); 
//add you deviceID 
      devicesList.add("APA91bELVJbxB_NLnLbTkkkX87SDdkJc6OfCN2slhC9t4cLq-KA32eGgiW4-Gi--ZEsEMKIh0AtYJMs5rQGswfm3cH1qK853WcpV98bkaplAaC5AiycDmifuVFSRl21vgf-Rqj0dCrFF"); 
         //devicesList.add("APA91bHIdM4XGqrjJLTuwCX5OOrTYG4ACXYEVkZDM1bPs5qFdzJP4Bpql-sZqyKB8BU7fDtdxB84aTygHLyASYg_XNY6lqrcA4wj4sZHJXGVFzz_0UEADMfFCx9NAfRZxunIYso_dkBa"); 
      //APA91bFA-i2l3iEMnIBs0JK80pTLHOsE7p1s-DysRpKGas1MQOVILyIs9xwY7soysSWGz5Uif68uXR6F5Xn0tCTYesv78uQZxhC310a1cvf8aFohhfMGY6awbOSg3t1GRz2i3U-8kVSF 
      // Use this line to send message without payload data 
      // Message message = new Message.Builder().build(); 

      // use this line to send message with payload data 
      Message message = new Message.Builder() 
        //.collapseKey("message") 
        //.timeToLive(241000) 
        .delayWhileIdle(true) 
        .addData("message", "Your message send") 
        .build(); 


        /**/ 
      // Use this code to send to a single device 
      // Result result = sender 
      // .send(message, 
      // "APA91bGiRaramjyohc2lKjAgFGpzBwtEmI8tJC30O89C2b3IjP1CuMeU1h9LMjKhmWuZwcXZjy1eqC4cE0tWBNt61Kx_SuMF6awzIt8WNq_4AfwflaVPHQ0wYHG_UX3snjp_U-5kJkmysdRlN6T8xChB1n3DtIq98w", 
      // 1); 

      // Use this for multicast messages 
      MulticastResult result = sender.send(message, devicesList, 1); 
      //sender.send(message, devicesList, 0); 

      System.out.println(result.toString()); 
      if (result.getResults() != null) { 
       int canonicalRegId = result.getCanonicalIds(); 
       if (canonicalRegId != 0) { 
       } 
      } else { 
       int error = result.getFailure(); 
       System.out.println(error); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 
} 

,你可以使用此代碼對客戶端部分:

public class GCMIntentService extends GCMBaseIntentService { 

    public GCMIntentService() { 
     super(""); 
     Log.d("GCMIntentService", senderId); 
    } 


    private void genNotification(Context context, String message) { 

     notify(context, message); 
     PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); 

     boolean isScreenOn = pm.isScreenOn(); 

     Log.e("screen on.................................", ""+isScreenOn); 

     if(isScreenOn==false) { 

      PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock"); 

      wl.acquire(10000); 
      PowerManager.WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyCpuLock"); 

      wl_cpu.acquire(10000); 
     } 
    } 

    public static void notify(Context context, String message) { 
     int icon = R.drawable.icon; 
     long when = System.currentTimeMillis(); 
     NotificationManager notificationManager = (NotificationManager) 
     context.getSystemService(Context.NOTIFICATION_SERVICE); 
     String title = context.getString(R.string.app_name); 
     Intent notificationIntent = new Intent(context, NotificationListView.class); 
     PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
     NotificationDataHelper helper = new NotificationDataHelper(context); 
     Notification notification; 
     notification = new Notification(icon, "message", when); 
      notificationIntent.putExtra("message", message); 
      notification.setLatestEventInfo(context, title, "message", intent); 
      notification.defaults |= Notification.DEFAULT_VIBRATE; 
      notification.setLatestEventInfo(context, title, msg, intent); 
     } 
     //set intent so it does not start a new activity 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     notificationIntent.setAction("notification"); 
     notificationManager.notify(0, notification); 

    } 


    @Override 
    protected void onError(Context arg0, String arg1) { 

    } 

    @Override 
    protected void onMessage(Context context, Intent intent) { 
     Log.d("GCM", "RECEIVED A MESSAGE"); 
     Log.d("GCM", "RECEIVED A MESSAGE"); 
     Log.d("GCM", "RECEIVED A MESSAGE"); 
     Log.d("GCM", "RECEIVED A MESSAGE"); 

      genNotification(context, intent.getStringExtra("message")); 

    } 

    @Override 
    protected void onRegistered(Context arg0, String arg1) { 

    } 

    @Override 
    protected void onUnregistered(Context arg0, String arg1) { 

    } 

} 

,並在第一你註冊設備http://developer.android.com/guide/google/gcm/index.html喜歡這個教程,並得到設備ID你可以發送信息給設備

private void registerGCM() { 
     GCMRegistrar.checkDevice(this); 
     //GCMRegistrar.unregister(this); 
     //Log.d("info", "unregistered....." + GCMRegistrar.getRegistrationId(this)); 
     GCMRegistrar.checkManifest(this); 
     if (GCMRegistrar.isRegistered(this)) { 
      Log.d("info", GCMRegistrar.getRegistrationId(this)); 
     } 
     final String regId = GCMRegistrar.getRegistrationId(this); 

     if (regId.equals("")) { 
      GCMRegistrar.register(this, "1013733918417"); 
      Log.d("info", GCMRegistrar.getRegistrationId(this)); 
     } else { 
      Log.d("info", "already registered as " + regId); 
     } 


      GCMIntentService.notify(this, null); 

    } 
+0

我發生了什麼是我的程序使得一個異步撥打電話。我的服務器是用PHP編寫的,但我不確定你的例子中用於服務器部分的id是什麼。而在客戶端部分,如果我只是粘貼該代碼,是否爲用戶在其設備上收到通知的相對標準代碼? – Genadinik

+0

順便說一下,到目前爲止,我沒有收集用戶的任何設備ID。我怎樣才能做到這一點?這是一個相對簡單的事情嗎? – Genadinik

+0

我想,你可以將設備ID保存到用戶數據庫 – Jamshid