我正在使用GCM向Android应用程序发送和接收推送通知。我是从这里给出的一个例子中创建这个应用程序的(Android的谷歌云消息GCM和推送通知)
这些是我正在使用的java文件
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class MainActivity extends Activity {
ShareExternalServer appUtil;
String regId;
AsyncTask shareRegidTask;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appUtil = new ShareExternalServer();
regId = getIntent().getStringExtra("regId");
Log.d("MainActivity", "regId: " + regId);
final Context context = this;
shareRegidTask = new AsyncTask() {
protected String doInBackground(Void... params) {
String result = appUtil.shareRegIdWithAppServer(context, regId);
return result;
}
protected void onPostExecute(String result) {
shareRegidTask = null;
Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_LONG).show();
}
};
shareRegidTask.execute(null, null, null);
}
}
最终语境=此;
shareRegidTask=new AsyncTask()
AsyncTask()上出错
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gms.gcm.GoogleCloudMessaging;
public class GCMNotificationIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GCMNotificationIntentService() {
super("GcmIntentService");
}
public static final String TAG = "GCMNotificationIntentService";
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
sendNotification("Deleted messages on server: "
+ extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
for (int i = 0; i < 3; i++) {
Log.i(TAG,
"Working... " + (i + 1) + "/5 @ "
+ SystemClock.elapsedRealtime());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
sendNotification("Message Received from Google GCM Server: "
+ extras.get(Config.MESSAGE_KEY));
Log.i(TAG, "Received: " + extras.toString());
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
Log.d(TAG, "Preparing to send notification...: " + msg);
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.gcm_cloud)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Log.d(TAG, "Notification sent successfully.");
}
}
通知兼容。Builder mBuilder=new NotificationCompat。建造商(
这). setSmallIcon(R.drawable.gcm_cloud)
.setContentTitle(“GCM通知”)
.setStyle(新NotificationCompat.BigTextStyle()。bigText(msg))
.setContentText(msg);
有错误。gcm_cloud
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import android.content.Context;
import android.util.Log;
public class ShareExternalServer {
public String shareRegIdWithAppServer(final Context context,
final String regId) {
String result = "";
Map paramsMap = new HashMap();
paramsMap.put("regId", regId);
try {
URL serverUrl = null;
try {
serverUrl = new URL(Config.APP_SERVER_URL);
} catch (MalformedURLException e) {
Log.e("AppUtil", "URL Connection Error: "
+ Config.APP_SERVER_URL, e);
result = "Invalid URL: " + Config.APP_SERVER_URL;
}
StringBuilder postBody = new StringBuilder();
Iterator> iterator = paramsMap.entrySet()
.iterator();
while (iterator.hasNext()) {
Entry param = iterator.next();
postBody.append(param.getKey()).append('=')
.append(param.getValue());
if (iterator.hasNext()) {
postBody.append('&');
}
}
String body = postBody.toString();
byte[] bytes = body.getBytes();
HttpURLConnection httpCon = null;
try {
httpCon = (HttpURLConnection) serverUrl.openConnection();
httpCon.setDoOutput(true);
httpCon.setUseCaches(false);
httpCon.setFixedLengthStreamingMode(bytes.length);
httpCon.setRequestMethod("POST");
httpCon.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
OutputStream out = httpCon.getOutputStream();
out.write(bytes);
out.close();
int status = httpCon.getResponseCode();
if (status == 200) {
result = "RegId shared with Application Server. RegId: "
+ regId;
} else {
result = "Post Failure." + " Status: " + status;
}
} finally {
if (httpCon != null) {
httpCon.disconnect();
}
}
} catch (IOException e) {
result = "Post Failure. Error in sharing with App Server.";
Log.e("AppUtil", "Error in sharing with App Server: " + e);
}
return result;
}
}
StringBuilder postBody=new StringBuilder();
迭代器
. iterator();
while(iterator.hasNext()){
Entry param=迭代器。下一步();
PostBody.append(param.getKey()). append('=')
.追加(param.getValue());
如果(iterator.hasNext()){
后体。附加('
} }
迭代器出错
我不明白为什么这些错误在增加,因为我在正确地遵循示例,请有人帮助我。
您的迭代器声明不正确(可能是拼写错误)。它应该是不带
我正在尝试在我的android应用程序中使用GCM服务。 为此,我使用Android留档从http://developer.android.com/guide/google/gcm/gcm.html 我用发送者id等创建了客户端注册过程,并在服务器端应用程序中使用注册id和发送者id发送消息。 当我通过Eclipse在手机中安装应用程序时,推送通知工作正常,因此我的发件人id是正确的。 然后,当我
更新:已弃用GCM,请使用FCM 如何将新的Google云消息集成到PHP后端?
我正在开发android GCM,以便我的应用程序向用户发送推送通知。我正在学习这个教程 http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/ 但是在本教程中,它表明我们可以向单个设备发送推送通知。但我想一次向所有用户发送推送通知。
我一直在研究如何将((谷歌推送通知))与PHP结合使用,但没有太多的工作示例/文档。。。 我正试图让它在一个内部网络上工作,这个网络不是通过HTTP对外开放的。 我能够从我们的外向型域通过HTTPS实现这一点,但内部尝试在Chrome控制台中告诉我,“API可能不再从不安全的来源使用”,我发现这意味着它需要HTTPS。 有人知道这方面可能的解决方法吗?欺骗HTTPS,或者允许它通过HTTP继续?我
我有一个包含用户名数据库的服务器。我还有一个Android应用程序,用户可以在服务器数据库中注册自己的用户名,也可以在服务器的GCM部分注册设备。 我目前正在服务器代码中使用GCM演示,它将多播一个推送通知到每个注册的设备。但是,我希望它能够将推送通知发送给某些用户,而不是每个注册的GCM设备。 我的第一个想法是将数据库中的每个用户与其GCM注册表关联起来。这样行吗?我读过一些关于regID更改或