2012-09-13 11 views
9

क्या किसी के पास नमूना जीसीएम सर्वर साइड और एंड्रॉइड प्रोजेक्ट है? अधिमानतः एक ट्यूटोरियल जो सबकुछ बताता है।Google क्लाउड मैसेजिंग नमूना

मैंने नमूना में शामिल एक को देखने की कोशिश की है हालांकि मैं इसे काम नहीं कर पा रहा हूं।

मेरे पास एक सी 2 डीएम प्रोजेक्ट है जो सर्वर की तरफ और एंड्रॉइड दोनों काम करता है, लेकिन मुझे नहीं पता कि इसे जीसीएम में कैसे परिवर्तित किया जाए।

मैं gcm का उपयोग किया जाएगा संदेशों

किसी भी मदद के पुश करने के लिए

+0

यहां बहुत अच्छा ट्यूटोरियल http://tech-papers.org/google-cloud-messaging-gcm-for-android-and-push-notifications-2/ –

उत्तर

4

सराहना की जाएगी बस का पालन करें इस tutorial

आशा है कि यह आप में मदद मिलेगी।

GCM सर्वर साइड (जावा कोड)

public class GCMServerJava { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 

    Sender sender = new Sender(enter your App id);// app id 



    Message message = new Message.Builder() 
    .collapseKey("1") 
    .timeToLive(3) 
    .delayWhileIdle(true) 
    .addData("message", 
      "this text will be seen in notification bar!!").build(); 
    Result result; 
    try { 


     result = sender.send(message,"registration id which client get after registering device with google gcm service", 1); 


     System.out.println(result.toString()); 

     Message message1 = new Message.Builder() 

     .build(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

}

+0

हालांकि यह लिंक प्रश्न का उत्तर दे सकता है, यह आवश्यक है कि आवश्यक यहां उत्तर के कुछ हिस्सों और संदर्भ के लिए लिंक प्रदान करते हैं। लिंक किए गए पृष्ठ में परिवर्तन होने पर लिंक-केवल उत्तर अमान्य हो सकते हैं। – hims056

+0

@ hims056 कि ट्यूटोरियल एंड्रॉइड डेवलपर से है जिसमें वे सभी बिंदु शामिल हैं। – Prachi

+0

स्टैक ओवरफ़्लो में केवल उत्तरों का लिंक स्वागत नहीं है। – hims056

1

कृपया नीचे दिए गए कोड की जाँच के लिए GCM एंड्रॉयड मेरे लिए काम करता है।

GCM एंड्रॉयड परियोजना: जावा

package com.example.samplegcm; 

public class CommonUtilities { 

    static final String SENDER_ID = "XXXXXXXXXXX"; // your project number from GCM 
} 

GCMIntentService.java

package com.example.samplegcm; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
import com.google.android.gcm.GCMBaseIntentService; 

public class GCMIntentService extends GCMBaseIntentService { 

    private static final String TAG = "GCMIntentService"; 

    public GCMIntentService() { 
     super(CommonUtilities.SENDER_ID); 
    } 

    @Override 
    protected void onRegistered(Context arg0, String registrationId) { 
     Log.i(TAG, "Device registered: regId = " + registrationId); 
    } 

    @Override 
    protected void onUnregistered(Context arg0, String arg1) { 
     Log.i(TAG, "unregistered = " + arg1); 
    } 

    @Override 
    protected void onMessage(Context context, Intent intent) { 
     Log.i(TAG, "new message= "); 
     String message = intent.getExtras().getString("message"); 
     generateNotification(context, message); 
    } 

    @Override 
    protected void onError(Context arg0, String errorId) { 
     Log.i(TAG, "Received error: " + errorId); 
    } 

    @Override 
    protected boolean onRecoverableError(Context context, String errorId) { 
     return super.onRecoverableError(context, errorId); 
    } 


    /** 
    * Issues a notification to inform the user that server has sent a message. 
    */ 
    private static void generateNotification(Context context, String message) { 
     int icon = R.drawable.ic_launcher; 
     long when = System.currentTimeMillis(); 
     NotificationManager notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(icon, message, when); 
     String title = context.getString(R.string.app_name); 
     Intent notificationIntent = new Intent(context, PushAndroidActivity.class); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
       | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent intent = PendingIntent.getActivity(context, 0, 
       notificationIntent, 0); 
     notification.setLatestEventInfo(context, title, message, intent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notificationManager.notify(0, notification); 
    } 

} 

CommonUtilities PushAndroidActivity.java

package com.example.samplegcm; 
    import android.app.Activity; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.widget.TextView; 
    import com.google.android.gcm.GCMRegistrar; 

    public class PushAndroidActivity extends Activity { 

    private String TAG = "** pushAndroidActivity **"; 
    private TextView mDisplay; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    checkNotNull(CommonUtilities.SENDER_ID, "SENDER_ID"); 

    GCMRegistrar.checkDevice(this); 
    GCMRegistrar.checkManifest(this); 

    setContentView(R.layout.activity_main); 
    mDisplay = (TextView) findViewById(R.id.display); 

    final String regId = GCMRegistrar.getRegistrationId(this); 
    Log.i(TAG, "registration id ===== "+regId); 

    if (regId.equals("")) { 
    GCMRegistrar.register(this, CommonUtilities.SENDER_ID); 
    } else { 
    Log.v(TAG, "Already registered"); 

    } 

    mDisplay.setText("Reg id is--> "+ regId); 
    } 

    private void checkNotNull(Object reference, String name) { 
    if (reference == null) { 
    throw new NullPointerException(
    getString(R.string.error_config, name)); 
    } 
    } 

    @Override 
    protected void onPause() { 
    super.onPause(); 
    GCMRegistrar.unregister(this); 
    } 
} 

Manifestfile

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.samplegcm" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <permission 
     android:name="com.example.samplegcm.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 

    <uses-permission android:name="com.example.samplegcm.permission.C2D_MESSAGE" /> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.samplegcm.PushAndroidActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <receiver 
      android:name="com.google.android.gcm.GCMBroadcastReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND" > 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
       <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 

       <category android:name="com.example.samplegcm" /> 
      </intent-filter> 
     </receiver> 

     <service android:name=".GCMIntentService" /> 
    </application> 

</manifest> 
+2

देखें यह बहिष्कृत है। –

+1

जौबर्ट - सी 2 डीएम बहिष्कृत है। जीसीएम सी 2 डीएम की जगह लेता है। –

1

यह लिंक देखें। यह लिंक आपको क्लाउड मैसेजिंग के लिए पूर्ण ट्यूटोरियल प्रदान करता है।

http://www.androidhub4you.com/2013/04/google-cloud-messaging-example-in.html

Android GCM उदाहरण के लिए
+1

ध्यान दें कि [केवल-लिंक उत्तर] (http://meta.stackoverflow.com/tags/link-only-answers/info) निराश हैं, इसलिए SO समाधान समाधान के लिए खोज का अंत बिंदु होना चाहिए (बनाम। फिर भी संदर्भों का एक और स्टॉपओवर, जो समय के साथ पुराना हो जाता है)। लिंक को संदर्भ के रूप में रखते हुए, यहां स्टैंड-अलोन सारांश जोड़ना पर विचार करें। – kleopatra

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^