- Forum-Beiträge: 9
31.10.2013, 22:32:16 via Website
31.10.2013 22:32:16 via Website
Ich versuche mich nun schon ganz schön lange daran eine GCM App ans laufen zu bekommen. Endlich hab ich ein Tutorial gefunden das ich verständlich fand. -> androidhiv. (android push notifications) (leider darf ich noch keine links posten)
Als ich es endlich Compilieren konnte, bekamm ich das Problem das ich mich zwar auf dem Server registrieren kann allerdings keine Nachricht zurück zum Emulator schicken kann.
LogCat gibt mir einen Fehler:
210-31 21:11:02.403: E/AndroidRuntime(833): FATAL EXCEPTION: IntentService[GCMIntentService-504651990869-1]
310-31 21:11:02.403: E/AndroidRuntime(833): java.lang.NullPointerException: println needs a message
410-31 21:11:02.403: E/AndroidRuntime(833): at android.util.Log.println_native(Native Method)
510-31 21:11:02.403: E/AndroidRuntime(833): at android.util.Log.d(Log.java:138)
610-31 21:11:02.403: E/AndroidRuntime(833): at com.example.send.GCMIntentService.onRegistered(GCMIntentService.java:35)
710-31 21:11:02.403: E/AndroidRuntime(833): at com.google.android.gcm.GCMBaseIntentService.handleRegistration(GCMBaseIntentService.java:296)
810-31 21:11:02.403: E/AndroidRuntime(833): at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:197)
910-31 21:11:02.403: E/AndroidRuntime(833): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
1010-31 21:11:02.403: E/AndroidRuntime(833): at android.os.Handler.dispatchMessage(Handler.java:99)
1110-31 21:11:02.403: E/AndroidRuntime(833): at android.os.Looper.loop(Looper.java:137)
1210-31 21:11:02.403: E/AndroidRuntime(833): at android.os.HandlerThread.run(HandlerThread.java:60)
1310-31 21:11:06.984: V/GCMRegistrar(833): Unregistering receiver
1410-31 21:11:07.004: E/UnRegister Receiver Error(833): > Receiver not registered: com.google.android.gcm.GCMBroadcastReceiver@412cf948
Hier noch der GCMIntentService.java:
2
3import android.app.Notification;
4import android.app.NotificationManager;
5import android.app.PendingIntent;
6import android.content.Context;
7import android.content.Intent;
8import android.net.Uri;
9import android.util.Log;
10
11
12
13import com.google.android.gcm.GCMBaseIntentService;
14
15
16
17import static com.example.send.CommonUtilities.SENDER_ID;
18import static com.example.send.CommonUtilities.displayMessage;
19
20public class GCMIntentService extends GCMBaseIntentService {
21
22 private static final String TAG = "GCMIntentService";
23
24 public GCMIntentService() {
25 super(SENDER_ID);
26 }
27
28 /**
29 * Method called on device registered
30 **/
31 @Override
32 protected void onRegistered(Context context, String registrationId) {
33 Log.i(TAG, "Device registered: regId = " + registrationId);
34 displayMessage(context, getString(R.string.gcm_registered));
35 Log.d("NAME", MainActivity.name);
36 ServerUtilities.register(context, MainActivity.name, MainActivity.email, registrationId);
37 }
38
39 /**
40 * Method called on device un registred
41 * */
42 @Override
43 protected void onUnregistered(Context context, String registrationId) {
44 Log.i(TAG, "Device unregistered");
45 displayMessage(context, getString(R.string.gcm_unregistered));
46 ServerUtilities.unregister(context, registrationId);
47 }
48
49 /**
50 * Method called on Receiving a new message
51 * */
52 @Override
53 protected void onMessage(Context context, Intent intent) {
54 Log.i(TAG, "Received message");
55 String message = intent.getExtras().getString("price");
56
57 displayMessage(context, message);
58 // notifies user
59 generateNotification(context, message);
60 }
61
62 /**
63 * Method called on receiving a deleted message
64 * */
65 @Override
66 protected void onDeletedMessages(Context context, int total) {
67 Log.i(TAG, "Received deleted messages notification");
68 String message = getString(R.string.gcm_deleted, total);
69 displayMessage(context, message);
70 // notifies user
71 generateNotification(context, message);
72 }
73
74 /**
75 * Method called on Error
76 * */
77 @Override
78 public void onError(Context context, String errorId) {
79 Log.i(TAG, "Received error: " + errorId);
80 displayMessage(context, getString(R.string.gcm_error, errorId));
81 }
82
83 @Override
84 protected boolean onRecoverableError(Context context, String errorId) {
85 // log message
86 Log.i(TAG, "Received recoverable error: " + errorId);
87 displayMessage(context, getString(R.string.gcm_recoverable_error,
88 errorId));
89 return super.onRecoverableError(context, errorId);
90 }
91
92 /**
93 * Issues a notification to inform the user that server has sent a message.
94 */
95 @SuppressWarnings("deprecation")
96 private static void generateNotification(Context context, String message) {
97 int icon = R.drawable.ic_launcher;
98 long when = System.currentTimeMillis();
99 NotificationManager notificationManager = (NotificationManager)
100 context.getSystemService(Context.NOTIFICATION_SERVICE);
101 Notification notification = new Notification(icon, message, when);
102
103 String title = context.getString(R.string.app_name);
104
105 Intent notificationIntent = new Intent(context, MainActivity.class);
106 // set intent so it does not start a new activity
107 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
108 Intent.FLAG_ACTIVITY_SINGLE_TOP);
109 PendingIntent intent =
110 PendingIntent.getActivity(context, 0, notificationIntent, 0);
111 notification.setLatestEventInfo(context, title, message, intent);
112 notification.flags |= Notification.FLAG_AUTO_CANCEL;
113
114 // Play default notification sound
115 notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "test.mp3");
116
117 // Vibrate if vibrate is enabled
118 notification.defaults |= Notification.DEFAULT_VIBRATE;
119 notificationManager.notify(0, notification);
120
121 }
122
123}
Und auf dem Server die Registration:
2
3// response json
4$json = array();
5
6/**
7 * Registering a user device
8 * Store reg id in users table
9 */
10if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["regId"])) {
11 $name = $_POST["name"];
12 $email = $_POST["email"];
13 $gcm_regid = $_POST["regId"]; // GCM Registration ID
14 // Store user details in db
15 include_once './db_functions.php';
16 include_once './GCM.php';
17
18 $db = new DB_Functions();
19 $gcm = new GCM();
20
21 $res = $db->storeUser($name, $email, $gcm_regid);
22
23 $registatoin_ids = array($gcm_regid);
24 $message = array("product" => "shirt");
25
26 $result = $gcm->send_notification($registatoin_ids, $message);
27
28 echo $result;
29} else {
30 // user details missing
31}
32?>
Meine Zweite Frage wäre, wie kann ich den sehen das die eingetippte message überhaupt an den Google Server gesendet wurde?
Hab viel gegooglet, bin aber leider nie so richtig fündig geworden.
Bitte seit nachsichtig falls ich was falsch mache, ist mein erster Eintrag hier =)
Danke im vorraus,
MfG Michi
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.