GCM推送通知实施

时间:2015-09-29 02:02:00

标签: java android python push-notification google-cloud-messaging

我似乎无法在我的应用程序中使用基本GCM包实现推送通知。我已经尝试了几个教程,并且到目前为止还无法使其正常工作。

我希望也许我可以得到一些帮助,当我从Ubuntu服务器发送时,我没有收到任何通知。

MainActivity



 
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context; 
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.android.gms.gcm.GcmListenerService;
public class MyGcmListener extends GcmListenerService
{
    private static final String TAG = "International Studies";
    @Override
    public void onMessageReceived(String from, Bundle data)
    {
        String message = data.getString("message");
        Log.d(TAG, "From: " + from);
        Log.d(TAG, "Message: " + message);
        Log.e("Message", "received ");

        sendNotification(message);
    }

    private void sendNotification(String message)
    {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("International Studies")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notificationBuilder.build());
    }

}




MyGcmListener



 
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import java.lang.Runnable;
import java.util.logging.Handler;
import java.util.logging.LogRecord;


public class MyGCMMessageHandler extends IntentService {
    String mes;
    private Handler handler;

    public MyGCMMessageHandler()
    {
        super("MyGCMMessageHandler");
    }

    public void onCreate()
    {
        super.onCreate();
        this.handler = new Handler() {
            @Override
            public void close() {

            }

            @Override
            public void flush() {

            }

            @Override
            public void publish(LogRecord record) {

            }
        };
    }

    protected void onHandleIntent(Intent intent)
    {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);
        this.mes = extras.getString("title");
        this.showToast();
        Log.i("GCM", "Received: (" + messageType + ") " + mes);
        MyGCMReceiver.completeWakefulIntent(intent);
    }

    public void showToast()
    {
        this.handler.post(new Runnable()
        {
            @Override
            public void run() {
                Toast.makeText(MyGCMMessageHandler.this.getApplicationContext(), MyGCMMessageHandler.this.mes, Toast.LENGTH_LONG).show();
            }
        });
    }
}




MyGcmMessageHandler



from gcm import *
gcm = GCM("")
data = {'message': 'This is a test push', 'param2': 'value2'}
reg_id = ''
gcm.plaintext_request(registration_id=reg_id, data=data)




推送脚本使用python-gcm

运行
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="abdroid.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <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=".MainActivity"
            android:configChanges="keyboardHidden|orientation"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <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.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="edu.csusb.internationalstudies" />
            </intent-filter>
        </receiver>
        <service android:name=".MyGcmListener" android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID"/>
            </intent-filter>
        </service>
    </application>

GCM中填充了API密钥,并在此应用程序中注册了我的手机的reg_id。

任何人都可以帮助我吗?我无法弄清楚出了什么问题。

编辑:清单。

&#13;
&#13;
{{1}}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

问题1:您的AndroidManifest.xml设置错误。这条线应该改变。

<category android:name="gcm.play.android.samples.com.gcmquickstart" /> 

gcm.play.android.samples.com.gcmquickstart应该用您自己的包替换。

问题2: 你有错误的进口。

import java.util.logging.Handler;

不是你想要的。需要的是android.os.Handler

  

Handler有两个主要用途:(1)安排消息和   runnables将在未来的某个点上执行; (2)至   将要在不同于自己的线程上执行的操作排入队列。