2012-10-12 7 views
0

에서 발생 후 단계를 http://tokudu.com/2010/how-to-implement-push-notifications-for-android/안드로이드 - 푸시 알림을 만드는 것은 뭔가 내가 TCP/IP를 사용하여 푸시 알림을 만드는 방법에 대해 읽고 한 서버

정말 난 단지 경고 할 필요가 "다소 오래된있을 수 있습니다 설명처럼 보인다 사용자가 "xyz 작업이 abc 앱에서 발생했습니다"

이전에 UrbanAirship을 사용하도록 권유 받았지만 그 방법에 어떤 영향을 미치는지 확신 할 수 없습니다. 보안 연결에서 알림을 보내야하기 때문입니다

알림을 보내려면 어떻게해야하나요? 전화 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) { 

    } 

} 

이 튜토리얼 얻을의 DeviceID처럼 먼저 등록 된 장치 http://developer.android.com/guide/google/gcm/index.html에서 장치에 대한 메시지를 보낼 수 있습니다.

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

내가 생각하기에, 당신은 userid를 위해 데이터베이스에 장치 ID를 저장할 수있다. – Jamshid

관련 문제