Permalink
Browse files

send Notification

  • Loading branch information...
1 parent 9bde4b6 commit f1c7975ff5a8d4413f82c91b0ad55a363fc02565 @susemi99 committed Jan 26, 2016
Showing with 30 additions and 0 deletions.
  1. +30 −0 app/src/main/java/kr/susemi99/gcm/services/GCMMessageListenerService.java
@@ -1,12 +1,24 @@
package kr.susemi99.gcm.services;
+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;
+import kr.susemi99.gcm.MainActivity;
+import kr.susemi99.gcm.R;
+
public class GCMMessageListenerService extends GcmListenerService
{
+ private static final int NOTIFICATION_ID = 91732846;
+
@Override
public void onMessageReceived(String from, Bundle data)
{
@@ -18,5 +30,23 @@ public void onMessageReceived(String from, Bundle data)
Object value = data.get(key);
Log.i("GCMMessageListenerService | onMessageReceived", String.format("%s : %s (%s)", key, value.toString(), value.getClass().getName()));
}
+
+ if (data.containsKey("message"))
+ sendNotification(data.getString("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, NOTIFICATION_ID, intent, PendingIntent.FLAG_ONE_SHOT);
+
+ Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
+ NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle(getString(R.string.app_name)).setContentText(message)
+ .setAutoCancel(true).setSound(defaultSoundUri).setPriority(NotificationCompat.PRIORITY_MAX)
+ .setContentIntent(pendingIntent);
+
+ NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+ notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}
}

0 comments on commit f1c7975

Please sign in to comment.