Mostrar texto de notificación en la barra de estado – Android
En mi aplicación necesito mostrar la notificación al usuario. El fragmento de código siguiente funcionó bien al mostrar el icono y el título del contenido en la barra de título del dispositivo Android.
var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager; var uiIntent = new Intent(this, typeof(MainActivity)); var notification = new Notification(Resource.Drawable.AppIcon, title); notification.Flags = NotificationFlags.AutoCancel; notification.SetLatestEventInfo(this, title, desc, PendingIntent.GetActivity(this, 0, uiIntent, 0)); notificationManager.Notify(1, notification);
Cuando intenté construir el paquete para desplegar la aplicación, consigo el error siguiente:
- Notificación push de Android GCM para aplicaciones de localización
- ¿Debería llamar a WakeLock antes de crear una notificación?
- No se reciben los caracteres unicode GCM de Android
- Utilizando GCM para enviar notificaciones en la aplicación, devuelve InvalidRegistration error
- ¿Qué servicios basados en la nube puedo usar para alojar el servidor XMPP requerido para conectarse a GCS CCS?
Android.App.Notification.SetLatestEventInfo (Android.Content.Context, cadena, cadena, Android.App.PendingIntent) 'está obsoleto:' desaprobado '
Así que encontré este fragmento de código que debería usar y muestra el icono en la barra de estado por no el título del contenido
Intent resultIntent = new Intent(this, typeof(MainActivity)); PendingIntent pi = PendingIntent.GetActivity(this, 0, resultIntent, 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(Forms.Context) .SetContentIntent(pi) .SetAutoCancel(true) .SetContentTitle(title) .SetSmallIcon(Resource.Drawable.AppIcon) .SetContentText(desc); //This is the icon to display NotificationManager nm = GetSystemService(Context.NotificationService) as NotificationManager; nm.Notify(_TipOfTheDayNotificationId, builder.Build());
¿Qué necesito para establecer el nuevo fragmento de código para mostrar el título del contenido en la barra de estado del dispositivo Android?
- ¿Cuáles son las razones por las que el ID de GCM no está disponible en algunos casos en android
- La aplicación recibe notificación duplicada mediante GCM después de reinstalar
- Problema con GCM. Manifiesto malformado como resultado de mayúsculas en el nombre del paquete
- Enviar mensaje GCM a Usuario particular
- Error de límite de tarifa excedido al utilizar Google Cloud Messaging API
- GCM no recibe el mensaje de abajo Android 4
- El anular el registro y el nuevo registro de mensajes GCM hace que dos regId sean válidos. ¿Es esto como se pretende?
- Dirección IP del servidor de mensajería de Google Cloud
Necesito agregar .setTicker()
al segundo fragmento de código para que se muestre el texto en la barra de estado del dispositivo Android
Debajo del código trabajó para mí. Sólo algunas correcciones en el segundo fragmento.
Intent resultIntent = new Intent(this, TestService.class); PendingIntent pi = PendingIntent.getActivity(this, 0, resultIntent, 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext()) .setContentIntent(pi) .setAutoCancel(true) .setContentTitle("title") .setSmallIcon(R.drawable.ic_notif) //add a 24x24 image to the drawable folder // and call it here .setContentText("desc"); NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.notify(0, builder.build());