【发布时间】:2020-10-11 17:37:46
【问题描述】:
以下代码是每次应用程序有新更新时显示的推送通知代码。
如何配置点击通知打开 PlayStore 窗口?也就是一个链接。
是否应该修改意图?
public static class NotificationServiceNuevaActualizacion extends IntentService {
private NotificationManager notificationManager;
private PendingIntent pendingIntent;
private int NOTIFICATION_ID = 4;
Notification notification;
public NotificationServiceNuevaActualizacion(String name) {
super(name);
}
public NotificationServiceNuevaActualizacion() {
super("SERVICE");
}
@TargetApi(Build.VERSION_CODES.O)
@Override
protected void onHandleIntent(Intent intent2) {
String NOTIFICATION_CHANNEL_ID = getApplicationContext().getString(R.string.app_name);
Context context = this.getApplicationContext();
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this, Principal.class);
Resources res = this.getResources();
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
String message = "New update";
if (SDK_INT >= Build.VERSION_CODES.O) {
final int NOTIFY_ID = 0; // ID of notification
String id = NOTIFICATION_CHANNEL_ID; // default_channel_id
String title = NOTIFICATION_CHANNEL_ID; // Default Channel
PendingIntent pendingIntent;
NotificationCompat.Builder builder;
NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (notifManager == null) {
notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = notifManager.getNotificationChannel(id);
if (mChannel == null) {
mChannel = new NotificationChannel(id, title, importance);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notifManager.createNotificationChannel(mChannel);
}
builder = new NotificationCompat.Builder(context, id);
mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentTitle(getString(R.string.app_name)).setCategory(Notification.CATEGORY_SERVICE)
.setSmallIcon(R.drawable.logo_utn) // required
.setContentText(message)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.icono))
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
Notification notification = builder.build();
notifManager.notify(NOTIFY_ID, notification);
startForeground(3, notification);
}
}
}
【问题讨论】:
标签: android notifications