package com.xxx.yyy.push;
import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.os.Build; import android.os.IBinder; import android.provider.MediaStore;
import com.xxx.yyy.R; import com.umeng.message.UTrack; import com.umeng.message.entity.UMessage;
import org.json.JSONException; import org.json.JSONObject;
import java.util.Random;
public class MyNotificationService extends Service {
public static UMessage oldMessage = null;
@Override public void onCreate() { super.onCreate(); }
@Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent == null) { return super.onStartCommand(null, flags, startId); } String message = intent.getStringExtra("UmengMsg"); try { UMessage msg = new UMessage(new JSONObject(message)); if (oldMessage != null) { UTrack.getInstance(getApplicationContext()).setClearPrevMessage(true); UTrack.getInstance(getApplicationContext()).trackMsgDismissed(oldMessage); } showNotification(msg); } catch (JSONException e) { e.printStackTrace(); } return super.onStartCommand(intent, flags, startId); }
private void showNotification(UMessage msg) { int id = new Random(System.nanoTime()).nextInt(); oldMessage = msg; NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (manager != null) manager.cancelAll();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { Notification.Builder mBuilder = new Notification.Builder(this, this.getPackageName()) .setContentTitle(msg.title) .setContentText(msg.text) .setTicker(msg.ticker) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(getClickPendingIntent(this, msg)) .setDeleteIntent(getDismissPendingIntent(this, msg)) .setChannelId(this.getPackageName()) .setAutoCancel(true);
if (manager != null) { CharSequence name = getResources().getString(R.string.app_name); String description = getResources().getString(R.string.app_description); NotificationChannel notificationChannel = new NotificationChannel(this.getPackageName(), name, NotificationManager.IMPORTANCE_HIGH); notificationChannel.setDescription(description); notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.RED); notificationChannel.enableVibration(true); notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 100}); manager.createNotificationChannel(notificationChannel); manager.notify(id, mBuilder.build()); } } else { Notification.Builder mBuilder = new Notification .Builder(this) .setContentTitle(msg.title) .setContentText(msg.text) .setTicker(msg.ticker) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(getClickPendingIntent(this, msg)) .setDeleteIntent(getDismissPendingIntent(this, msg)) .setWhen(System.currentTimeMillis()) .setDefaults(Notification.DEFAULT_ALL) .setSound(MediaStore.Audio.Media.INTERNAL_CONTENT_URI) .setAutoCancel(true); if (manager != null) manager.notify(id, mBuilder.build()); } }
public PendingIntent getClickPendingIntent(Context context, UMessage msg) { Intent clickIntent = new Intent(); clickIntent.setClass(context, NotificationBroadcast.class); clickIntent.putExtra(NotificationBroadcast.EXTRA_KEY_MSG, msg.getRaw().toString()); clickIntent.putExtra(NotificationBroadcast.EXTRA_KEY_ACTION, NotificationBroadcast.ACTION_CLICK);
return PendingIntent.getBroadcast(context, (int) (System.currentTimeMillis()), clickIntent, PendingIntent.FLAG_CANCEL_CURRENT); }
public PendingIntent getDismissPendingIntent(Context context, UMessage msg) { Intent deleteIntent = new Intent(); deleteIntent.setClass(context, NotificationBroadcast.class); deleteIntent.putExtra(NotificationBroadcast.EXTRA_KEY_MSG, msg.getRaw().toString()); deleteIntent.putExtra( NotificationBroadcast.EXTRA_KEY_ACTION, NotificationBroadcast.ACTION_DISMISS); return PendingIntent.getBroadcast(context, (int) (System.currentTimeMillis() + 1), deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT); }
@Override public IBinder onBind(Intent intent) { return null; } }
|