Created
August 19, 2016 13:51
-
-
Save anonymous/5ad672abfb34690fe075fe304ae1019c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codeartmobile.solncetur.gcm; | |
import android.annotation.TargetApi; | |
import android.app.Notification; | |
import android.app.NotificationManager; | |
import android.app.PendingIntent; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.pm.PackageManager; | |
import android.content.pm.ResolveInfo; | |
import android.media.RingtoneManager; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.support.v4.app.NotificationCompat; | |
import com.codeartmobile.solncetur.MainActivity; | |
import com.codeartmobile.solncetur.R; | |
import com.codeartmobile.solncetur.notifications.WebViewActivity; | |
import com.google.android.gms.gcm.GcmListenerService; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.util.List; | |
/** | |
* Created by ${timkin} on ${date}. | |
*/ | |
public class SolnceTurGcmListenerService extends GcmListenerService { | |
private static final String TAG = "MyGcmListenerService"; | |
public static void setBadge(Context context, int count) { | |
String launcherClassName = getLauncherClassName(context); | |
if (launcherClassName == null) { | |
return; | |
} | |
Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE"); | |
intent.putExtra("badge_count", count); | |
intent.putExtra("badge_count_package_name", context.getPackageName()); | |
intent.putExtra("badge_count_class_name", launcherClassName); | |
context.sendBroadcast(intent); | |
} | |
public static String getLauncherClassName(Context context) { | |
PackageManager pm = context.getPackageManager(); | |
Intent intent = new Intent(Intent.ACTION_MAIN); | |
intent.addCategory(Intent.CATEGORY_LAUNCHER); | |
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0); | |
for (ResolveInfo resolveInfo : resolveInfos) { | |
String pkgName = resolveInfo.activityInfo.applicationInfo.packageName; | |
if (pkgName.equalsIgnoreCase(context.getPackageName())) { | |
String className = resolveInfo.activityInfo.name; | |
return className; | |
} | |
} | |
return null; | |
} | |
@Override | |
public void onMessageReceived(String from, Bundle data) { | |
String url = data.getString("st"); | |
if (url != null) { | |
try { | |
JSONObject link = new JSONObject(url); | |
url = link.getString("link"); | |
String body = data.getString("gcm.notification.body"); | |
String title = data.getString("gcm.notification.title"); | |
sendNotificationForWebView(body, title, url); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} else { | |
String body = data.getString("gcm.notification.body"); | |
String title = data.getString("gcm.notification.title"); | |
String badge = data.getString("gcm.notification.badge"); | |
sendNotification(body, title, badge); | |
} | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
private void sendNotification(String body, String title, String badge) { | |
Intent intent = new Intent(this, MainActivity.class); | |
intent.putExtra("from_notify", true); | |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
if (badge == null) | |
badge = "0"; | |
if (title == null) | |
title = ""; | |
if (body == null) | |
body = ""; | |
PendingIntent pendingIntent = PendingIntent.getActivity(this, Integer.valueOf(badge), intent, | |
PendingIntent.FLAG_ONE_SHOT); | |
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); | |
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) | |
.setSmallIcon(R.drawable.ic_stat_name) | |
.setContentTitle(title) | |
.setContentText(body) | |
.setPriority(Notification.PRIORITY_HIGH) | |
.setShowWhen(true) | |
.setVisibility(Notification.VISIBILITY_PUBLIC) | |
.setAutoCancel(true) | |
.setSound(defaultSoundUri) | |
.setContentIntent(pendingIntent); | |
NotificationManager notificationManager = | |
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
notificationManager.notify(0, notificationBuilder.build()); | |
if (!badge.equals("0")) | |
setBadge(getBaseContext(), Integer.valueOf(badge)); | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
private void sendNotificationForWebView(String body, String title, String url) { | |
Intent intent = new Intent(this, WebViewActivity.class); | |
intent.putExtra("url", url); | |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
if (title == null) | |
title = ""; | |
if (body == null) | |
body = ""; | |
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_name) | |
.setContentTitle(title) | |
.setContentText(body) | |
.setPriority(Notification.PRIORITY_HIGH) | |
.setAutoCancel(true) | |
.setVisibility(Notification.VISIBILITY_PUBLIC) | |
.setShowWhen(true) | |
.setSound(defaultSoundUri) | |
.setContentIntent(pendingIntent); | |
NotificationManager notificationManager = | |
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
notificationManager.notify(0, notificationBuilder.build()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment