【问题标题】:Phonegap Firebase Push NotificationsPhonegap Firebase 推送通知
【发布时间】:2018-09-26 09:27:11
【问题描述】:

我有一个 Android/ios 应用程序,我使用我的浏览器和 php 在数据库中插入文章,然后应用程序使用 ajax 从数据库中推送数据。

我需要的是,当我在 php(服务器端)上发布文章并插入时,它会向手机发送通知。

我的应用 js 上有这个:

const push = PushNotification.init({
            android: {
                vibrate: "true",
                alert: "true",
                badge: "true",
                sound: "true"
            },
            browser: {
            },
            ios: {
                vibrate: "true",
                alert: "true",
                badge: "true",
                sound: "true"
            },
            windows: {}
        });
            push.on('registration', function(data) {
            //alert(data.registrationId);
            var registrationId=data.registrationId;
            var dataString ="id_usuario="+registrationId;
            if($.trim(registrationId).length>0){
                $.ajax({
                    type: "POST",
                    url: "https://pizzarte.com/app/registarid.php",
                    data: dataString,
                    crossDomain: true,
                    cache: false,
                })
            }
        });

            push.on('notification', function(data) {
            //alert(data.title+" Message: grgregregerg" +data.message);
            });

            push.on('error', function(e) {
            alert(e);
            });

因此,当用户启动应用程序时,它会保存该设备的注册 ID,并将其存储在数据库中。这工作得很好。现在在服务器端,我提取所有已注册的 ID,并尝试发送通知:

<?php

// API access key from Google API's Console - server key here https://console.developers.google.com/apis/credentials?authuser=2&project=project_name
define( 'API_ACCESS_KEY', 'this is my api access key, im using the server key');
$registrationIds = array("device_ids");

var_dump ($registrationIds);

// prep the bundle
$msg = array
(
    'message'   => 'here is a message. message',
    'title'     => 'This is a title. title',
    'subtitle'  => 'This is a subtitle. subtitle',
    'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
    'vibrate'   => 1,
    'sound'     => 1
);

$fields = array
(
    'registration_ids'  => $registrationIds,
    'data'          => $msg
);

$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
);

var_dump ($headers);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );

echo $result;

但我总是收到错误“未经授权 错误 401"

在我的 config.xml 我也有这个:

<plugin name="phonegap-plugin-push" spec="2.0.0" />
<variable name="SENDER_ID" value="xxxxxxxxxxxxx" />
</plugin>

<platform name="android">
    <preference name="android-minSdkVersion" value="14" />
    <allow-intent href="market:*" />
    <resource-file src="google-services.json" target="google-services.json" />
</platform>

我在这里做错了什么?

【问题讨论】:

    标签: php firebase firebase-cloud-messaging phonegap


    【解决方案1】:

    在 config.xml 中更改它

    <plugin name="phonegap-plugin-push" spec="~2.1.2" >
            <variable name="FCM_VERSION" value="11.0.1" />
      </plugin>
    

    然后改变下一个

    <platform name="android">
        <preference name="android-minSdkVersion" value="14" />
        <allow-intent href="market:*" />
        <resource-file src="google-services.json" target="google-services.json" />
    </platform>
    

    <platform name="android">
        <preference name="android-minSdkVersion" value="16" />
        <allow-intent href="market:*" />
        <resource-file src="google-services.json" target="app/google-services.json" />
    </platform>
    

    value="14" 到 value="16"

    target="google-services.json" 必须是 target="app/google-services.json"

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2018-12-17
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 2017-03-05
      相关资源
      最近更新 更多