【问题标题】:open specific page using firebase api使用 firebase api 打开特定页面
【发布时间】:2017-12-01 12:33:06
【问题描述】:

我正在使用以下代码向设备发送推送通知。

      var nTitle = "New message from " + $rootScope.clients_firstName;
      var to = "DeviceToken is here";
      var notification = {
        'title': nTitle,
        //'body': 'Click here to more details...',
        'icon': 'firebase-logo.png',
        'click_action': 'openapp'
      };

      var key = 'your key';
      fetch('https://fcm.googleapis.com/fcm/send', {
      'method': 'POST',
        'headers': {
        'Authorization': 'key=' + key,
        'Content-Type': 'application/json'
      },
      'body': JSON.stringify({
        'notification': data,
        'to': to
      })
      }).then(function(response) {
        //console.log("res ", response);
      }).catch(function(error) {
        console.error("err ", error);
     });

推送通知在设备上成功发送。

但是当我点击通知时,应该打开特定页面。

例如'click_action' : 'openapp/somepage'

那么当我点击 pushnotification 时,'somepage' 应该是打开的。

AndroidManifest.xml

<activity android:name="MainActivity" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">

        <intent-filter>
            <action android:name="openapp" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>

</activity>

我的项目结构

--platforms
--plugins
--www
  --app
    --about
      --about.html
      --about.ctrl.js
    --product
      --product.html
      --product.ctrl.js
  --css
  --js
  --lib
  index.html

如果我点击通知并想打开产品或关于页面,那我该怎么办?

这里有什么问题?请告诉我正确的解决方案。

【问题讨论】:

    标签: android firebase ionic-framework


    【解决方案1】:

    我知道你问的是 Ionic 1。但我只知道 Ionic 2。希望这会对你有所帮助。

    我认为您应该在通知的附加数据部分添加有关需要打开的页面的信息。像这样的:

    'body': JSON.stringify({
            'notification': data,
            'to': to,
            'data' : {
               'action' : 'Needed page'
            }
          })
    

    然后你应该在 subscribe 方法上捕获它。类似的东西:

    fcm.onNotification().subscribe(data=>{
      console.log("data", data);
      if (data['action'] && data['action'] == 'Needed page'){
        this.navCtrl.push('Needed page'); //Use Navigation controller to open needed page
      }
    })
    

    对于原生 cordova FCM 插件:

    FCMPlugin.onNotification(function(data){
        console.log("data", data);
        if (data['action'] && data['action'] == 'Needed page'){
          this.navCtrl.push('Needed page'); //Use Navigation controller to open needed page
        }
    });
    

    【讨论】:

    • 我正在使用 firebase api。这行得通吗?看我的代码,很简单。
    • 您需要尝试使用控制台日志。我认为关于推送通知正文的第一个示例将起作用。第二个不,因为我为 Ionic 2 编写了示例。但是 ionic 1 的算法是相同的。您只需要通过 fcm cordova 插件github.com/fechanique/cordova-plugin-fcm 订阅通知。使用 onNotification 方法。
    • 更新了答案。
    【解决方案2】:

    如果您使用的是 Ionic Push 插件,则可以使用

    $scope.$on('cloud:push:notification', function(event, data) {
            var msg = data.message;
            var appState = msg.app;
            var appAsleep = appState.asleep;
            var appClosed = appState.closed;
    
    
            if (appAsleep || appClosed) {               
                $state.go("your state");
            } else {
               //if app is open while receiving push
            }
    
        });
    

    【讨论】:

    • 如果我使用插件,那么我可以提供帮助。但我没有使用任何插件。我使用了 firebase api。
    【解决方案3】:

    在推送负载中传递您的 url,并使用以下代码打开该 url 引用的特定屏幕:

    $rootScope.$on("$cordovaLocalNotification:click", function(notification, state) {
        var data = JSON.parse(state.data);
        window.location.href = data.url;
    });
    

    【讨论】:

    • 我没有使用任何插件。我使用了 firebase api。
    【解决方案4】:

    你问我here。 如果您使用我的解决方案并在 onReceive(Context context, Intent intent) 方法中手动创建通知,则必须将待处理的意图放入通知生成器。

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    //init builder with icon, text, any other params
    
    Intent activityIntent = new Intent(context, Activity.class);
    activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); //it is needed if you creating an activity from not activity context
    PendingIntent pendingIntent = PendingIntent.getActivity(context, REQUEST_CODE, activityIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    builder.setContentIntent(pendingIntent);
    
    Notification notification = builder.build();
    notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS;
    NotificationManagerCompat.from(context).notify(notificationId, notification);
    

    【讨论】:

    • 现在,如果我从任何页面/视图关闭应用程序。当我收到通知时,应用程序会从默认页面(最后打开的页面)打开。但我想打开我自己的页面,而不是默认页面。
    【解决方案5】:

    要让您的应用在后台时可点击通知,您需要在通知负载中添加click_action 属性。

    请查看 Firebase 文档的 section

    此外,当您定义click_action 属性时,您还需要在您希望启动的活动的&lt;intent-filter&gt; 中具有相应的&lt;action&gt; 属性。

    这个video 非常详细地解释了它。

    不过,请注意,如果您从 Firebase 控制台发送通知,则无法设置 click__action 属性。只有在您从自己的管理服务器发送通知或使用 Firebase Cloud Functions 时,您才能这样做。

    最后,在启动的活动中,您可以使用 data 属性设置额外的Data(也显示在我上面链接的同一文档中)。当您通过单击通知启动应用程序时,您可以使用getIntent() 获取通知数据。查看this answer 了解更多详情。

    问题更新后编辑:-

    不要将&lt;intent-filter&gt; 放在MainActivity 中,而是将过滤器放在您想要在单击通知时打开的活动的&lt;activity&gt; 标记中。一个例子如下:-

    <activity android:name="ProductDetailsActivity" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
    
        <intent-filter>
            <action android:name="openapp" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>
    

    现在您指定的活动已打开,您可以使用getIntent() 从通知的data 部分获取信息。

    例如,如果您的通知负载具有以下结构,

    payload = {
      notification: {
        title: `You ordered a new product`,
        click_action : 'HANDLE_NOTIFICATION',
    
      },
      data : {
            product_id : 'ABC98292',
            type : `Clothes`,
            product_name : 'Cotton spring shirt'
        }
    };
    

    然后您可以使用getIntent().getStringsExtra("product_id") 等从通知中获取product_id

    这样,您将打开所需的活动,并可以使用从通知中获得的相关详细信息填充它,而无需使用任何其他框架。

    【讨论】:

    • 我上面的代码正在运行。当我点击通知时,应用程序也打开了。但我想打开一个特定的页面。表示如果通知与“产品”相关,则“产品”页面将打开,如果通知与“聊天”相关,则“聊天”页面将打开。明白吗?
    • 您仅发布了清单的 &lt;intent_filter&gt; 部分。告诉我那个过滤器属于哪个Activity。或者只是在您的答案中发布清单的&lt;activity&gt; 部分。
    • 现在我收到一个错误 Uncaught ReferenceError: getIntent is not defined.
    • 使用这个this.cordova.getActivity().getIntent();
    • 又是错误Uncaught TypeError: this.cordova.getActivity is not a function
    猜你喜欢
    • 1970-01-01
    • 2020-11-17
    • 2015-09-26
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    • 2015-12-05
    • 2017-12-23
    • 2013-07-12
    相关资源
    最近更新 更多