简体   繁体   中英

Show Heads up notification when app is not open

I use fcm and heads up notification will show when app is open but not show when app is not open or killed. How to handle heads up notification when app is not open?

Doc say :

With Android 5.0 (API level 21), notifications can appear in a small floating window (also called a heads-up notification) when the device is active (that is, the device is unlocked and its screen is on). These notifications appear similar to the compact form of your notification, except that the heads-up notification also shows action buttons. Users can act on, or dismiss, a heads-up notification without leaving the current app.

As per Doc, If you want heads-up notification you have to create your own as below :

notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
if (Build.VERSION.SDK_INT >= 21) notificationBuilder.setVibrate(new long[0]);

Don't abuse heads-up notification. See here for when to use heads-up notification:

MAX: For critical and urgent notifications that alert the user to a condition that is time-critical or needs to be resolved before they can continue with a particular task.

HIGH: Primarily for important communication, such as messages or chat events with content that is particularly interesting for the user. High-priority notifications trigger the heads-up notification display.

Additional note from HERE

Update :

To override GCM listener service :

<service android:name=".MyGcmListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
</service>

FCM would be :

<intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>

Then override method :

GCM :

public class MyGcmListenerService
        extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
    ... create your heads-up notification here.
}

FCM :

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */    
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
    ... create your heads-up notification here.
}

Can't post in comment, so here it is. Try this, i had tested :

private void test() {
    Intent intent;
    intent = new Intent(this, SplashScreenActivity.class);
    Bundle bundle = new Bundle();
    bundle.putBoolean("isDisplayAlert", true);
    bundle.putString("NOTIFICATION_DATA", "data");
    intent.putExtras(bundle);
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
            new Random().nextInt(), intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_location)
            .setContentTitle("Title")
            .setContentText("Body")
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setOnlyAlertOnce(true)
            .setFullScreenIntent(pendingIntent, true);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
    notificationBuilder.setVibrate(new long[0]);
    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM