GCM Gruppen Chat: Nur mein Telefon empfängt Benachrichtigungen

  • Antworten:17
  • Bentwortet
Bastian Seidemann
  • Forum-Beiträge: 137

29.04.2014, 09:58:53 via Website

Hi Leute,

Habe eine Frage.

Ich habe in meine App GCM eingebunden und alles funktioniert super auf meinem Handy.

Andere Geräte (getestet auf Samsung Galaxy S3 und Sony Xperia Z) können senden aber empfangen keine Push Benachrichtigungen.

Registriert sind alle 3 Geräte in meiner MySQL DB.

Woran kann sowas liegen?

Ich bin verzweifelt. :(

Antworten
Bastian Seidemann
  • Forum-Beiträge: 137

29.04.2014, 14:04:46 via Website

Aberbei mir geht es ja.

Okay hier das Manifest:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android&quot; package="com.example.juzko" android:versionCode="1" android:versionName="1.0" >

&lt;uses-sdk
    android:minSdkVersion=&quot;14&quot;
    android:targetSdkVersion=&quot;18&quot; /&gt;

&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.RECEIVE_BOOT_COMPLETED&quot; /&gt;
&lt;uses-permission android:name=&quot;com.android.alarm.permission.SET_ALARM&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.VIBRATE&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.GET_TASKS&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.BATTERY_STATS&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.CAMERA&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.ACCESS_WIFI_STATE&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.GET_ACCOUNTS&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.WAKE_LOCK&quot; /&gt;
&lt;uses-permission android:name=&quot;com.google.android.c2dm.permission.RECEIVE&quot; /&gt;


&lt;permission android:name=&quot;com.example.juzko.permission.C2D_MESSAGE&quot;
android:protectionLevel=&quot;signature&quot; /&gt;

<uses-permission android:name="com.example.juzko.permission.C2D_MESSAGE" />

&lt;application
    android:allowBackup=&quot;true&quot;
    android:icon=&quot;@drawable/ic_launcher&quot;
    android:label=&quot;@string/app_name&quot;
    android:theme=&quot;@style/AppTheme&quot; &gt;
    &lt;activity
        android:name=&quot;com.example.juzko.MainActivity&quot;
        android:label=&quot;@string/app_name&quot;
        android:screenOrientation=&quot;portrait&quot;
        android:windowSoftInputMode=&quot;adjustUnspecified&quot; &gt;
    &lt;/activity&gt;
    &lt;activity
        android:name=&quot;com.example.juzko.Chatroom&quot;
        android:label=&quot;@string/title_activity_mitglieder&quot;
        android:screenOrientation=&quot;portrait&quot;
        android:theme=&quot;@android:style/Theme.Holo.Light.NoActionBar.Fullscreen&quot; &gt;
    &lt;/activity&gt;
    &lt;activity
        android:name=&quot;com.example.juzko.Aktivitaeten&quot;
        android:label=&quot;@string/title_activity_aktivitaeten&quot;
        android:screenOrientation=&quot;portrait&quot;
        android:theme=&quot;@android:style/Theme.Holo.Light.NoActionBar.Fullscreen&quot; &gt;
    &lt;/activity&gt;
    &lt;activity
        android:name=&quot;com.example.juzko.Bierliste&quot;
        android:label=&quot;@string/title_activity_bierliste&quot;
        android:screenOrientation=&quot;portrait&quot;
        android:theme=&quot;@android:style/Theme.Holo.Light.NoActionBar.Fullscreen&quot; &gt;
    &lt;/activity&gt;
    &lt;activity
        android:name=&quot;com.example.juzko.SPS&quot;
        android:label=&quot;@string/title_activity_sps&quot;
        android:screenOrientation=&quot;portrait&quot;
        android:theme=&quot;@android:style/Theme.Holo.Light.NoActionBar.Fullscreen&quot; &gt;
        &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

            &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;
    &lt;activity
        android:name=&quot;com.example.juzko.Login&quot;
        android:label=&quot;@string/title_activity_login&quot;
        android:screenOrientation=&quot;portrait&quot;
        android:theme=&quot;@android:style/Theme.DeviceDefault.Light.NoActionBar.Fullscreen&quot;
        android:windowSoftInputMode=&quot;adjustPan&quot; &gt;
    &lt;/activity&gt;

    &lt;activity
        android:name=&quot;com.example.juzko.StartAtBootServiceReceiver&quot;
        android:label=&quot;@string/title_activity_start_at_boot_service_receiver&quot; &gt;
    &lt;/activity&gt;

     &lt;meta-data android:name=&quot;com.google.android.gms.version&quot;
       android:value=&quot;@integer/google_play_services_version&quot; /&gt;




    &lt;receiver
        android:name=&quot;com.example.juzko.StartAtBootServiceReceiver&quot;
        android:enabled=&quot;true&quot;
        android:exported=&quot;false&quot; &gt;
        &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.intent.action.BOOT_COMPLETED&quot; /&gt;
        &lt;/intent-filter&gt;
    &lt;/receiver&gt;

    &lt;activity
        android:name=&quot;com.example.juzko.PicPreview&quot;
        android:label=&quot;@string/title_activity_pic_preview&quot; 
        android:theme=&quot;@android:style/Theme.DeviceDefault.Light.NoActionBar.Fullscreen&quot;&gt;
    &lt;/activity&gt;

    &lt;receiver
        android:name=&quot;.GcmBroadcastReceiver&quot;
        android:permission=&quot;com.google.android.c2dm.permission.SEND&quot; &gt;
        &lt;intent-filter&gt;
            &lt;!-- Receives the actual messages. --&gt;
            &lt;action android:name=&quot;com.google.android.c2dm.intent.RECEIVE&quot; /&gt;
            &lt;!-- Receives the registration id. --&gt;
            &lt;action android:name=&quot;com.google.android.c2dm.intent.REGISTRATION&quot; /&gt;
            &lt;category android:name=&quot;com.example.juzko&quot; /&gt;
        &lt;/intent-filter&gt;
    &lt;/receiver&gt;
    &lt;service android:name=&quot;.GcmIntentService&quot; 
        android:enabled=&quot;true&quot;/&gt;
&lt;/application&gt;

</manifest>

ist da was verkehrt?

— geändert am 29.04.2014, 14:06:43

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

29.04.2014, 15:31:14 via Website

Nur am Rande werden bei euch SpitzeKlammern als "& lt;" html Tags angezeigt?

Ich finde das nerft ganz schön..

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Bastian Seidemann
  • Forum-Beiträge: 137

29.04.2014, 15:32:07 via Website

leider ja :(

Antworten
Bastian Seidemann
  • Forum-Beiträge: 137

30.04.2014, 09:24:13 via Website

Um nochmal zum eigentlichen Thema zurückzukehren:

GCMIntentservice:

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.graphics.BitmapFactory; import android.os.Bundle; import android.os.SystemClock; import android.os.Vibrator; 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 {

String globalname;

public static final int NOTIFICATION_ID = 1;
private static final String TAG = &quot;GcmIntentService&quot;;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;

public GcmIntentService() {
    super(&quot;GcmIntentService&quot;);

}

@Override
protected void onHandleIntent(Intent intent) {

    IntentFilter filter = new IntentFilter();
    filter.addAction(&quot;Update&quot;);

    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) {

        Log.i(&quot;wird durchlaufen&quot;, &quot;wird durchlaufen&quot;);
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
                .equals(messageType)) {
            sendNotification(&quot;Send error: &quot; + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
                .equals(messageType)) {
            sendNotification(&quot;Deleted messages on server: &quot;
                    + extras.toString());

        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
                .equals(messageType)) {

            for (int i = 0; i &lt; 5; i++) {
                Log.i(TAG,
                        &quot;Working... &quot; + (i + 1) + &quot;/5 @ &quot;
                                + SystemClock.elapsedRealtime());
                try {
                    Thread.sleep(400);
                } catch (InterruptedException e) {
                }
            }

            if (isForeground(&quot;com.example.juzko&quot;) == false) {
                globalname = extras.getString(&quot;Name&quot;);
                sendNotification(extras.getString(&quot;Notice&quot;));
            }

            Intent i = new Intent(&quot;speedExceeded&quot;);
            i.putExtra(&quot;nachricht&quot;, extras.getString(&quot;Notice&quot;));
            i.putExtra(&quot;zeit&quot;, extras.getString(&quot;Zeit&quot;));
            i.putExtra(&quot;name&quot;, extras.getString(&quot;Name&quot;));
            LocalBroadcastManager.getInstance(this).sendBroadcast(i);

            Log.i(TAG, &quot;Received: &quot; + extras.toString());

        }
    }

    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

private void sendNotification(String 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)

            .setContentTitle(&quot;JUZ Chat&quot;)
            .setSmallIcon(R.drawable.koortsschild)
            .setLargeIcon(
                    BitmapFactory.decodeResource(getResources(),
                            R.drawable.koortsschild))
            .setStyle(
                    new NotificationCompat.BigTextStyle()
                            .bigText(globalname + &quot;: &quot; + msg))
            .setContentText(msg).setAutoCancel(true);

    Vibrator v = (Vibrator) getApplication().getSystemService(
            Context.VIBRATOR_SERVICE);

    v.vibrate(500);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

public boolean isForeground(String PackageName) {

    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

    List&lt;ActivityManager.RunningTaskInfo&gt; task = manager.getRunningTasks(1);

    ComponentName componentInfo = task.get(0).topActivity;

    if (componentInfo.getPackageName().equals(PackageName))
        return true;

    return false;
}

}

GCMBroadcastReceiver:

package com.example.juzko;

import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.support.v4.content.WakefulBroadcastReceiver;

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

}

Manifest Siehe oben^^

Antworten
Bastian Seidemann
  • Forum-Beiträge: 137

01.05.2014, 10:13:30 via App

Keiner eine Idee?

Antworten
Andy N.
  • Forum-Beiträge: 22.375

01.05.2014, 10:18:48 via App

Hallo Bastian,

Bitte nicht pushen, ich bin mir sicher, dass sich hier jemand melden wird, sobald er/sie eine Antwort für dich hat... ;)

Viele Grüße,
Andy


OnePlus 3 (Resurrection Remix 5.8.2)
LG G Watch

Regeln | unsere Mods & Admins

Antworten
Bastian Seidemann
  • Forum-Beiträge: 137

01.05.2014, 11:19:41 via Website

Habe noch etwas hinzuzufügen was das Problem höchstwahrscheinlich schwer eingrenzt.

Wenn ich die Registrierungs IDs auf dem Server entferne und eines der Geräte die nicht funktioniert haben als aller erstes Gerät registriere dann geht es. Wenn Ich dann mein Telefon wo es vorher geklappt hat als zweite Reg-Id registriere geht es da nicht.

Als würde er nur die erste Reg-Id akzeptieren.

EDIT:

Habe das PHP Script zum verarbeiten der Nachricht ausgewertet.

Trotz "SELECT * FROM tblregistration" (und 2 Einträgen in der DB, also 2 regids)

übergibt er dem GCM Server nur eine (nämlich die erste) Reg-Id.

— geändert am 01.05.2014, 12:04:23

Antworten
Ludy
  • Admin
  • Forum-Beiträge: 7.958

01.05.2014, 12:08:55 via App

Ich kann mich irren aber muss nicht jedes Gerät im gcm registriert sein!?
Jedes Gerät kommt dann eine individuelle Id zugewiesen oder seh ich das falsch? Hast du mal in der Dokumentation geschaut was die dazu sagt?

Gruß Ludy (App Entwickler)

Mein Beitrag hat dir geholfen? Lass doch ein "Danke" da.☺

☕ Buy Me A Coffee ☕

Lebensmittelwarnung-App

✨Meine Wunschliste✨

📲Telegram NextPit News📲

Antworten
Bastian Seidemann
  • Forum-Beiträge: 137

01.05.2014, 12:10:28 via Website

Richtig so ist es.

Ich habe hier 2 physische Geräte neben mir liegen.

Beide sind registriert und haben eine ID auf meinem Server.

Bei einem "SELECT * " auf meine DB wird aber nur die erste ID verwendet.

Antworten
Bastian Seidemann
  • Forum-Beiträge: 137

01.05.2014, 12:24:26 via Website

Habs jetzt :)

Problem war das der gesamte GCM Sender Vorgang in der while Schleife mit drin war.

So sollte es richtig aussehen und geht auch jetzt mit allen Geräten :)

 &lt;?php

$nachricht=$_POST['nachricht'];
$nachricht=utf8_encode($nachricht);
$name=$_POST['name'];
$zeit=$_POST['zeit'];

$con = mysql_connect("XXX.XXX.de", "XXXXX","XXXXXX";);
if(!$con){
die('MySQL connection failed');
}

$db = mysql_select_db("XXXXXXXX";);
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(&quot;Notice&quot; =&gt; $nachricht, &quot;Zeit&quot; =&gt; $zeit, &quot;Name&quot; =&gt; $name);
     $fields = array(
         'registration_ids' =&gt; $registration_ids,
     'delay_while_idle' =&gt; true,
         'data' =&gt; $message,
     );


echo json_encode($registration_ids);


     $headers = array(
         'Authorization: key=AIzaSyC-bSOHlTXXXXXXXXXXXVHre7zQf5w',
         '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;

?>

Antworten
Gelöschter Account
  • Forum-Beiträge: 437

02.07.2014, 18:00:52 via Website

<hello> 

Hallo Zusammen,

Die spitze Klammern werden nun richtig angezeigt!

VG Caspar

Antworten