谷歌云消息示例

10

有没有人有一个示例 GCM 服务器端和 Android 项目?最好是一份解释一切的教程。

我尝试查看示例中包含的内容,但我无法使它工作。

我有一个 c2dm 项目,可以在服务器端和 Android 上使用,但我不知道如何将其转换为 GCM。

我将使用 GCM 推送消息。

任何帮助都将不胜感激。


请查看此链接。该链接为您提供了云消息传递的完整教程。http://www.androidhub4you.com/2013/04/google-cloud-messaging-example-in.html - AKSH
非常好的教程在这里:http://tech-papers.org/google-cloud-messaging-gcm-for-android-and-push-notifications-2/ - Balwinder SIngh
3个回答

4

请按照教程进行操作。

希望能对你有所帮助。

GCM 服务端 (Java 代码)

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

}


我需要一个完全可用的示例。这个似乎对我不起作用。 - Tuffy G
@TuffyG,你能解释一下你的问题吗? - Prachi
无法从我的PHP脚本接收我发送的推送消息。这就是为什么我希望两者都能正常工作,以便我可以进行比较。 - Tuffy G
只需检查您的客户端代码是否正确,为此只需创建一个简单的Java服务器并通过Java服务器推送通知。 - Prachi
有关如何设置的任何教程? - Tuffy G

1
请查看以下的GCM Android代码。这对我很有效。
GCM Android项目:

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 .java

package com.example.samplegcm;

public class CommonUtilities {

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

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>

1
Joubert - C2DM已被弃用。GCM取代了C2DM。 - Siddharth_Vyas

-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接