return (
<html>
<head>
<Script
src=".page.js"
defer
/>
<Script id="onesignal-init">
{`
window.OneSignalDeferred = window.OneSignalDeferred || [];
OneSignalDeferred.push(async function(OneSignal) {
await OneSignal.init({
appId: "${process.env.NEXT_PUBLIC_ONESIGNAL_APP_ID}",
safari_web_id: "",
allowLocalhostAsSecureOrigin: true,
notifyButton: { enable: true },
welcomeNotification: {
title: "LocalHost",
message: "Bildirimleri başarıyla etkinleştirdiniz!"
},
promptOptions: {
slidedown: {
prompts: [
{
type: "push",
autoPrompt: true,
text: {
actionMessage: "Önemli güncellemeler için bildirimleri açmak ister misiniz?",
acceptButton: "İZİN VER",
cancelButton: "DAHA SONRA"
}
}
]
}
}
});
// Kullanıcı bildirimleri kabul ettiğinde Player ID'yi al
OneSignal.Notifications.addEventListener("subscriptionChange", async function(isSubscribed) {
if (isSubscribed) {
const userId = await OneSignal.getUserId();
console.log("Kullanıcının Player ID’si:", userId);
// Backend'e Player ID'yi gönder
/*
fetch("/api/savePlayerId", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ playerId: userId, userId: "12345" })
});
*/
}
});
});
`}
</Script>
</head>
<body>
<div className="flex h-screen">
<SideNav />
<div className="flex-1 flex flex-col">
<TopNav />
<main className="flex-1 overflow-y-auto bg-[#F8F9FB]">{children}</main>
</div>
</div>
</body>
</html>
);
I loaded OneSignal on layout.tsx
And I tried this on another page:
<button onClick={async () => {
try {
if (!window.OneSignal) {
console.error("OneSignal yüklenmedi.");
return;
}
window.OneSignalDeferred = window.OneSignalDeferred || [];
window.OneSignalDeferred.push(async (OneSignal) => {
const userId = await OneSignal.getUserId();
if (userId) {
console.log("Player ID:", userId);
} else {
console.log("Kullanıcı bildirimlere abone olmamış.");
}
});
} catch (error) {
console.error("Player ID alınırken bir hata oluştu:", error);
}
}}>
Pull Player Id
</button>
But this thing is not worked.
Error:
Uncaught (in promise) TypeError: OneSignal.getUserId is not a function
How can i solve this problem i need users player id for push notifications.
Am i using wrong version or something else? a