Android how to show notification on screen

前端 未结 5 2106
情歌与酒
情歌与酒 2020-12-05 23:06

I\'ve been working on push notifications and I am able to implement it and display it on status bar, the problem I am facing is that I want to display it even if the phone i

相关标签:
5条回答
  • 2020-12-05 23:35

    Create Notification using NotificationCompat.Builder

    NotificationCompat.Builder mBuilder =   new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher) // notification icon
                .setContentTitle("Notification!") // title for notification
                .setContentText("Hello word") // message for notification
                .setAutoCancel(true); // clear notification after click
    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pi = PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK);
    mBuilder.setContentIntent(pi);
    NotificationManager mNotificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, mBuilder.build());
    

    Push Notification on locked Screen http://www.hongkiat.com/blog/android-lock-screen-notifications/

    0 讨论(0)
  • 2020-12-05 23:44

    I fixed this by adding this line to notification builder

    builder.setOngoing(true);
    

    It will also make notification not cancelable by user, but it solves the problem.

    Credits to: Marian Klühspies (link)

    0 讨论(0)
  • 2020-12-05 23:48

    The notifications you have seen may actually be widgets placed on a custom widget host lockscreen.

    If you look at the android platform source code for InstallWidgetReceiver as late as 4.4.3 here:

    https://android.googlesource.com/platform/packages/apps/Launcher3/+/master/src/com/android/launcher3/InstallWidgetReceiver.java

    You will see this note by the author:

    /** * We will likely flesh this out later, to handle allow external apps to place widgets, but for now, * we just want to expose the action around for checking elsewhere. */

    And you can see that InstallWidgetReceiver.java is in fact not fleshed out by google in the same way as InstallShortCutReceiver.java is. So it seems at least up to 4.4.3 you cant add widgets to the native lock screen in the same way that you can for example add a shortcut to the homescreen using InstallShortCutReceiver.

    Unless you build your own lockscreen app as a widget host and the user installs in lieu of the native you may be out of luck using a widget.

    Another approach however is to just us an activity that sets getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

    This will display your activity whether the screen is locked or not. Dismissing this activity when the screen is locked will display the locked screen.

    0 讨论(0)
  • 2020-12-05 23:53

    Create Notification using NotificationCompat.Builder but make sure to put visibility to public like

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
            builder
            .setContentTitle("Title")
            .setContentText("content")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);//to show content in lock screen
    
    0 讨论(0)
  • 2020-12-05 23:56

    Have you tried creating the alertdialog with a flag? The flag_show_when_locked should do the trick. Please refer to this thread, you should find a more detailed answer here. Android Lock Screen Widget

    0 讨论(0)
提交回复
热议问题