Local Notifications in Android?

后端 未结 3 485
半阙折子戏
半阙折子戏 2020-12-07 14:12

In iOS, a \"Local Notification\" is used by an app when it is in the background, to inform the user that something has happened, that they might want to pay attention to:

3条回答
  •  囚心锁ツ
    2020-12-07 14:33

    Use NotificationCompat.Builder if you are targeting old APIs too.

        Intent intent = new Intent(ctx, HomeActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
        NotificationCompat.Builder b = new NotificationCompat.Builder(ctx);
    
        b.setAutoCancel(true)
         .setDefaults(Notification.DEFAULT_ALL)
         .setWhen(System.currentTimeMillis())         
         .setSmallIcon(R.drawable.ic_launcher)
         .setTicker("Hearty365")            
         .setContentTitle("Default notification")
         .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
         .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
         .setContentIntent(contentIntent)
         .setContentInfo("Info");
    
    
        NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, b.build());
    

提交回复
热议问题