簡體   English   中英

GCM推送通知實施

[英]GCM Push Notification Implementation

我似乎在使用我的應用程序中的基本GCM包實現推送通知時遇到了麻煩。 我已經嘗試了幾個教程,並且到目前為止還無法使其正常工作。

我希望也許我可以得到一些幫助,當我從Ubuntu服務器發送時,我沒有收到任何通知。

主要活動

 import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import com.google.android.gms.gcm.GoogleCloudMessaging; import com.google.android.gms.iid.InstanceID; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; public class MainActivity extends ActionBarActivity { private String RegID = ""; private Context context = null; private GoogleCloudMessaging gcm = null; private InstanceID instanceID = null; private BroadcastReceiver mHandleMesageReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = getApplicationContext(); if(RegID.isEmpty()) { register RegInBG = new register(); RegInBG.execute("e"); post task = new post(); task.execute(RegID); mHandleMesageReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { } }; } } /*Runnable task to be used to register app in the GCM service for push notifications, made by Devin Adams */ public class register extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { try { if (instanceID == null) { instanceID = InstanceID.getInstance(context); } if (gcm == null) { gcm = GoogleCloudMessaging.getInstance(context); } RegID = instanceID.getToken(getString(R.string.sender_id), gcm.INSTANCE_ID_SCOPE, null); Log.e("***********", "************"); Log.e("***********", "************"); Log.e("RegID", RegID); Log.e("***********", "************"); Log.e("***********", "************"); } catch (IOException ex) { Log.e("***********", "************"); Log.e("***********", "************"); Log.e("Error", ex.getMessage()); Log.e("***********", "************"); Log.e("***********", "************"); } return "sd"; } } public class post extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { try { String tempurl = getString(R.string.server_url); Uri.Builder b = Uri.parse(tempurl).buildUpon(); String url = b.build().toString(); HttpURLConnection connection = (HttpURLConnection) ((new URL(url))).openConnection(); connection.setRequestMethod("POST"); connection.setDoInput(true); connection.setDoOutput(true); connection.connect(); connection.getOutputStream().write((params[0]).getBytes()); connection.disconnect(); } catch (IOException ex) { Log.e("Error", ex.getMessage()); } return "getg"; } } } 

MyGcmListener

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

MyGcmMessageHandler

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

推送腳本使用python-gcm運行

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)

GCM中填充了API密鑰,並在此應用程序中注冊了我的手機的reg_id。

誰能幫我嗎? 我無法弄清楚出了什么問題。

編輯:清單。

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

問題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)將要在不同於自己的線程上執行的操作排入隊列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM