Java tutorial
/** * Copyright 2015 Google Inc. All Rights Reserved. * <p/> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.jongrakko.zipsuri.gcm; 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 net.jongrakko.zipsuri.R; import net.jongrakko.zipsuri.activity.IntroActivity; import net.jongrakko.zipsuri.dialog.PushDialog; public class ZipsuriGcmListenerService extends GcmListenerService { private static final String TAG = "gcm"; public static final String TYPE_NEW_POST = "post"; public static final String TYPE_NEW_BID = "bid"; public static final String TYPE_NEW_ANSWER = "answer"; public static final String TYPE_NEW_QUESTION = "question"; public static final String TYPE_NEW_PUBLIC_ANSWER = "public_answer"; public static final String TYPE_POST_REVIEW = "review"; public static final String TYPE_AWARDING_POST = "awarding"; public static final String TYPE_REVISE_AWARDING_POST = "revise_awarding"; public static final String INTENT_TYPE = "type"; public static final String INTENT_ID = "id"; /** * Called when message is received. * * @param from SenderID of the sender. * @param data Data bundle containing message data as key/value pairs. * For Set of keys use data.keySet(). */ // [START receive_message] @Override public void onMessageReceived(String from, Bundle data) { String title = data.getString("title"); String userId = data.getString("user_id"); String message = data.getString("message"); String type = data.getString("type"); String id = data.getString("id"); sendNotification(title, userId, message, type, id); } /** * Create and show a simple notification containing the received GCM message. * * @param message GCM message received. */ private void sendNotification(String title, String userId, String message, String type, String id) { Intent intent = new Intent(this, IntroActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra(INTENT_TYPE, type); intent.putExtra(INTENT_ID, id); PendingIntent pendingIntent = PendingIntent.getActivity(this, 7050, intent, PendingIntent.FLAG_UPDATE_CURRENT); String content; if (type.equals(TYPE_NEW_ANSWER) || type.equals(TYPE_NEW_QUESTION)) { if (message == null) { message = "()"; } content = userId + " : " + message; } else { content = message; } Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_stat_name).setContentTitle(title).setContentText(content) .setAutoCancel(true).setSound(defaultSoundUri).setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); notificationManager.cancel(7050); notificationManager.notify(7050, notificationBuilder.build()); Intent dialogIntent = new Intent(this, PushDialog.class); dialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); dialogIntent.putExtra(INTENT_TYPE, type); dialogIntent.putExtra(INTENT_ID, id); dialogIntent.putExtra(PushDialog.INTENT_CONTENT, content); dialogIntent.putExtra(PushDialog.INTENT_TITLE, title); startActivity(dialogIntent); } }