duanfu4446 2018-05-23 11:44
浏览 329

用firebase和php推送通知

I have a problem with firebase and php for push notifications the push notification appears on the user mobile but the push itself if clicked on the user mobile does nothing.

I expect it to lead to the page of the content but the push doesnt get to the user

var config = {
    apiKey: "AIzaSyAXavmNUsgT7UgRIQEdweFXIhBrYTGPvOY",
    authDomain: "tech-miz.firebaseapp.com",
    databaseURL: "https://tech-miz.firebaseio.com",
    projectId: "tech-miz",
    storageBucket: "tech-miz.appspot.com",
    messagingSenderId: "942114086591"
};

firebase.initializeApp(config);
// Retrieve Firebase Messaging object.
const messaging = firebase.messaging();
messaging.requestPermission()
.then(function() {
    console.log('Notification permission granted.');
    // TODO(developer): Retrieve an Instance ID token for use with FCM.
    /*if(isTokenSentToServer()){
        console.log("Token already saved");

    } else {
        getRegToken();
    }*/
    getRegToken();
})
.catch(function(err) {
    console.log('Unable to get permission to notify.', err);
});

function getRegToken() {
    // Get Instance ID token. Initially this makes a network call, once retrieved
    // subsequent calls to getToken will return from cache.
    messaging.getToken()
    .then(function(currentToken) {
        if (currentToken) {
        //sendTokenToServer(currentToken);
        // console.log(currentToken);

        saveToken(currentToken);
        setTokenSentToServer(true);
        //updateUIForPushEnabled(currentToken);
        } else {
            // Show permission request.
            console.log('No Instance ID token available. Request permission to generate one.');
        // Show permission UI.
            setTokenSentToServer(false);
        }
    })
    .catch(function(err) {
        console.log('An error occurred while retrieving token. ', err);
        // showToken('Error retrieving Instance ID token. ', err);
        setTokenSentToServer(false);
    });
}

function setTokenSentToServer(sent) {
    window.localStorage.setItem('sentToServer', sent ? 1 : 0);
}
function isTokenSentToServer() {
    return window.localStorage.getItem('sentToServer') == 1;
}
function saveToken(currentToken){
   $.ajax({
       type: "POST",
       url: "blog/ajax/save.push.notification.php",
       data: "token=" + currentToken,
       success: function (response) {
           console.log(response);
       }
   });
}

I need the push notification to be clickable and lead the user to the page where the push notification is notifying the user about

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?