Tag: android notifications

升级到Android 8.1后,startForeground将失败

将手机升级到8.1 Developer Preview后,我的后台服务不能再正常启动。 在我长时间运行的服务中,我实现了一个startForeground方法来启动在创建时调用的正在进行的通知。 @TargetApi(Build.VERSION_CODES.O) private fun startForeground() { // Safe call, handled by compat lib. val notificationBuilder = NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID) val notification = notificationBuilder.setOngoing(true) .setSmallIcon(R.drawable.ic_launcher_foreground) .build() startForeground(101, notification) } 错误信息: 11-28 11:47:53.349 24704-24704/$PACKAGE_NAMEE/AndroidRuntime: FATAL EXCEPTION: main Process: $PACKAGE_NAME, PID: 24704 android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=My channel pri=0 […]

Android – 正确的通知方式和服务

我正在开发一个Android应用程序,它必须通过蓝牙保持连接并监听来电,如果它在前台运行。 当我得到蓝牙连接时保持服务正常运行,并在断开连接时停止服务,从而能够正常工作。 Service : public class ConnectionService extends Service { private final int NOTIFICATION_ID = 327; @Override public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; } @Override public IBinder onBind(Intent intent) { throw new UnsupportedOperationException(“Not yet implemented”); } @Override public void onCreate() { //Creates notification Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent […]

当应用程序处于非活动状态时,通知显示android图标,而不显示应用

我遵循默认的firebase指南来设置android推送通知,但是通知会显示android图标,当应用程序在api 26中处于非活动状态时,当它显示应用程序图标时,也不会在redmi note 4设备中显示通知。 使用本指南转换为kotlin: https ://firebase.google.com/docs/cloud-messaging/android/client

TaskStackBuilder addParentStack()方法不起作用

我需要像这样的流程:当通知出现,用户点击它,activityB应该启动,比用户点击后退按钮它应该去activityA,我的问题是,当用户点击通知和activityB正在lanched,堆栈是空,如果用户点击后退按钮,应用程序的行为就像按下主页按钮,点击启动器图标后,启动启动器活动,流程就像第一次启动。 另外看到谷歌的官方文件如何正确使用通知与保存活动更好的导航体验,但没有帮助。 这是显示通知的代码 private fun sendNotification() { val mBuilder = NotificationCompat.Builder(this) .setContentTitle(name) .setContentText(messageBody) val resultIntent = Intent(this, activityB::class.java) val stackBuilder = TaskStackBuilder.create(this) stackBuilder.addParentStack(activityA::class.java) stackBuilder.addNextIntent(resultIntent) val resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT) mBuilder.setContentIntent(resultPendingIntent) mNotificationManager.notify(mId, mBuilder.build()) } 这里是清单文件 <activity android:name=".activityB" android:parentActivityName="activityA" /> <activity android:name="activityA"/> 我不明白,我错过了什么

点击通知动作的触发功能

我正在写一个应用程序在Kotlin应该显示一个通知与一个行动按钮。 我有一堂课。 我们称之为NotificationClass 这个类通过fun createNotification()为我创建通知 这一切工作正常。 但是我想为通知添加一个按钮,点击的时候会触发一个名为notificationActionClicked() NotificationClass函数。 有什么办法可以做到这一点? (通知类不是一个安卓服务,它只是一个实用程序类。)

Android Kotlin – 无法安排与警报管理器通知未来的日期

我有一些kotlin代码来安排Android notifcation 。 这是我的代码看起来像 @Throws(ParseException::class) fun scheduleNotification(context: Context, info: Info) { val notificationIntent = Intent(context, NotificationPublisher::class.java) notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, System.currentTimeMillis()) notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, getNotification(context, info)) val pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT) val date = Utils.sdfWithTime.parse(String.format("%s %s", info.date, // yyyy-MM-dd format info.status)) // HH:mm format val delay = SharedPreferenceUtils.getPreferenceValueInteger(context, PreferenceKeys.alarmDelayPref) // date is in UTC val futureInMillis = date.time […]