简体   繁体   中英

PushSharp Google Cloud Messaging Heads-up notification

I'm developing an application for Android and iOS and I'm using PushSharp (on server-side) to send push notifications to both platform. In particular I'm using (for Android) the Firebase platform (FCM).

Following this guide I was able to send push notification to an Android device setting icon and sound too but I think there is a problem. When the notification arrives it doesn't being shown as Heads-up notification but only as status bar notification.

To be clear, I would:

在此处输入图像描述

but i see only the application icon that appears on the status bar.

How can I tell to FCM to show my notification as Head-Up notification similary to what I obtain with the following code?

NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_media_play)
                        .setContentTitle("My notification")
                        .setContentText("Hello World!")
                        .setDefaults(Notification.DEFAULT_ALL)
                        .setPriority(Notification.PRIORITY_HIGH);

I have fixed it by installing following version of react-native-push-notification

npm install zo0r/react-native-push-notification.git

in your index.android.js

function init(){
    PushNotification.configure({
        onNotification:async (notification)=>{
                if(!notification.userInteraction && !notification.foreground){
                    PushNotification.localNotification({
                      message: "you message"
                    });
                  }
               ,
                requestPermissions:true,
                senderID:"31************",
                popInitialNotification:false
        })
}
  1. you must create a channel in your MainActivity.java.
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
+import android.os.Build;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

+      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {    
+        NotificationChannel notificationChannel = new 
+        NotificationChannel("500", "MainChannel", 
+        NotificationManager.IMPORTANCE_HIGH);
+        notificationChannel.setShowBadge(true);
+        notificationChannel.setDescription("Test Notifications");
+        notificationChannel.enableVibration(true);
+        notificationChannel.enableLights(true);
+        notificationChannel.setVibrationPattern(new long[]{400, 200, 400});
+        NotificationManager manager = getSystemService(NotificationManager.class);
+         manager.createNotificationChannel(notificationChannel);
+      }
  1. Add the channelId to your server: an example with node.js
        await admin
            .messaging()
            .send({
                android: {
                    priority: 'high',
                    notification: {
                        sound: 'default',
                        title: 'your title',
                        body: 'your message',
                        imageUrl: 'img-uri',
                        priority: 'high', // this is importnat
                        channelId: '500', // the channelId we created in MainActivity.java
                    },
                },
                apns: {
                    payload: {
                        aps: {
                            contentAvailable: true,
                        },
                    },
                    headers: {
                        'apns-push-type': 'background',
                        'apns-priority': '5',
                        'apns-topic': '', // your app bundle identifier
                    },
                },
                topic: //topic or token,
                data: {//send a custom data here to your client},
                notification: {
                    title: 'your title',
                    body:'your message',
                    imageUrl: 'img-url',
                },

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