Java tutorial
/* Copyright 2015 Google Inc. All Rights Reserved. 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 http://www.apache.org/licenses/LICENSE-2.0 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 com.ct.fitorto.gcm; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.text.TextUtils; import com.ct.fitorto.R; import com.ct.fitorto.activity.HomeActivity; import com.ct.fitorto.activity.NotificationActivity; import com.ct.fitorto.preferences.PreferenceManager; import com.ct.fitorto.utils.ApplicationData; import com.google.android.gms.gcm.GcmListenerService; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; /** * Service used for receiving GCM messages. When a message is received this service will log it. */ public class GcmService extends GcmListenerService { private String title; private int notificationId; PreferenceManager manager; public GcmService() { } @Override public void onMessageReceived(String from, Bundle data) { manager = new PreferenceManager(this); String msg = data.getString("message"); title = data.getString("title"); // notificationId = manager.getPreferenceIntValues(ApplicationData.NOTIFICATION_ID); sendNotification(msg, title); } private void sendDeepLinkNotification(String msg, String title, String id) { /* Intent intent = new Intent(this, VideoDetailsActivity.class); intent.putExtra(ApplicationData.VIDEOS_ID, id); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, 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(title) .setContentText(msg) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0 *//* ID of notification *//*, notificationBuilder.build());*/ } @Override public void onDeletedMessages() { sendNotification("Deleted messages on server", title); } @Override public void onMessageSent(String msgId) { sendNotification("Upstream message sent. Id=" + msgId, title); } @Override public void onSendError(String msgId, String error) { sendNotification("Upstream message send error. Id=" + msgId + ", error" + error, title); } // Put the message into a notification and post it. // This is just one simple example of what you might choose to do with // a GCM message. private void sendNotification(String msg, String title) { Intent intent = new Intent(this, NotificationActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_stat_fitorto) .setLargeIcon((BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))) .setContentTitle(title).setContentText(msg).setAutoCancel(true).setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notificationBuilder.build()); //manager.putPreferenceIntValues(ApplicationData.NOTIFICATION_ID, (notificationId + 1)); } }