- Forum-Beiträge: 137
09.05.2014, 17:24:26 via Website
09.05.2014 17:24:26 via Website
Hallo Leute,
Ich habe eine Chat Applikation geschrieben welche GCM benutzt um Push Benachrichtigungen über neue Nachrichten zu Empfangen.
Das ganze ist ein Gruppenchat.
Wenn Ich im selben Netz bin mit z.b einem zweiten Android Gerät und meiner App empfange ich Nachrichten mit allem drum und dran.
Auch wenn das nicht der Fall ist kommen sporadisch Nachrichten an.
Meistens ist es aber so dass Ich meine Internetverbindung trennen und neu verbinden muss.
Danach lädt er sofort die aktuelle Nachricht vom GCM Server nach.
Aber nicht vorher.
Hier mein Code:
PHP Server Side:
<?php
$nachricht=$_POST['nachricht'];
$nachricht=utf8_encode($nachricht);
$name=$_POST['name'];
$zeit=$_POST['zeit'];
$con = mysql_connect("XXXXXXXXXX", "XXXXXXXXXXX","XXXXXXXXXXX"
if(!$con){
die('MySQL connection failed');
}
$db = mysql_select_db("DB1640880"
if(!$db){
die('Database selection failed');
}
$registration_ids = array();
$sql = "SELECT * FROM tblregistration";
$result = mysql_query($sql, $con);
while($row = mysql_fetch_assoc($result)){
array_push($registration_ids, $row['registration_id']);
}
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$message = array("Notice" => $nachricht, "Zeit" => $zeit, "Name" => $name);
$fields = array(
'registration_ids' => $registration_ids,
'delay_while_idle' => true,
'data' => $message,
);
echo json_encode($registration_ids);
$headers = array(
'Authorization: key=AIzaSyD4-XXXXXXXXXXc0Qa5fnl3lqMlfBE',
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
?>
GCM Broadcastreceiver:
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
GCM Intent Service:
package com.example.juzko;
import java.util.List;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import android.app.ActivityManager;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.os.Vibrator;
import android.provider.MediaStore.Audio;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.widget.ListView;
public class GcmIntentService extends IntentService {
public SharedPreferences loginprefs;
private static final String TAG = "GcmIntentService";
public GcmIntentService() {
super("XXXXXXXXXXX"
}
@Override
protected void onHandleIntent(Intent intent) {
loginprefs = getSharedPreferences("login", 0);
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
Log.i("wird durchlaufen", "wird durchlaufen"
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)) {
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
for (int i = 0; i < 3; i++) {
Log.i(TAG,
"Working... " + (i + 1) + "/3 @ "
+ SystemClock.elapsedRealtime());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
Intent i = new Intent("speedExceeded"
i.putExtra("nachricht", extras.getString("Notice");
i.putExtra("zeit", extras.getString("Zeit");
i.putExtra("name", extras.getString("Name");
LocalBroadcastManager.getInstance(this).sendBroadcast(i);
Log.i(TAG, "Received: " + extras.toString());
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.juzko"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BATTERY_STATS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="com.example.juzko.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.juzko.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.juzko.MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustUnspecified" >
</activity>
<activity
android:name="com.example.juzko.Chatroom"
android:label="@string/title_activity_mitglieder"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen" >
</activity>
<activity
android:name="com.example.juzko.Aktivitaeten"
android:label="@string/title_activity_aktivitaeten"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen" >
</activity>
<activity
android:name="com.example.juzko.Bierliste"
android:label="@string/title_activity_bierliste"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen" >
</activity>
<activity
android:name="com.example.juzko.SPS"
android:label="@string/title_activity_sps"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.juzko.Login"
android:label="@string/title_activity_login"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar.Fullscreen"
android:windowSoftInputMode="adjustPan" >
</activity>
<activity
android:name="com.example.juzko.StartAtBootServiceReceiver"
android:label="@string/title_activity_start_at_boot_service_receiver" >
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<receiver
android:name="com.example.juzko.StartAtBootServiceReceiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity
android:name="com.example.juzko.PicPreview"
android:label="@string/title_activity_pic_preview"
android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar.Fullscreen" >
</activity>
<receiver
android:name="com.example.juzko.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.juzko" />
</intent-filter>
</receiver>
<receiver android:name="com.example.juzko.NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<service
android:name="com.example.juzko.GcmIntentService"
android:enabled="true" />
<activity
android:name="com.example.juzko.BierAdapter"
android:label="@string/title_activity_bier_adapter" >
</activity>
</application>
</manifest>
Hat jemand eine idee woran es scheitert?
Hab delay_while_idle sowohl auf true als auch false versucht.
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.