gpt4 book ai didi

java - OneSignal 操作按钮

转载 作者:行者123 更新时间:2023-12-02 02:17:47 27 4
gpt4 key购买 nike

我对应用程序开发、Java 和 StackOverflow 完全陌生,这是我的第一个 Android 应用程序。

我已经实现了 OneSignal 来发送推送通知。我只是想在通知中添加对操作按钮的支持。

我希望当用户触摸 ID 为 posts 的操作按钮时启动 SplashActivity,并希望在用户触摸 ID 为 app 的按钮时在 Web 浏览器中启动 URL。

所以,这是我的 ApplicationClass.java:

package com.ananya.brokenhearts;

import android.app.Application;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import com.onesignal.OSNotificationAction;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;
import org.json.JSONObject;

public class ApplicationClass extends Application
{
@Override
public void onCreate()
{
super.onCreate();

OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.unsubscribeWhenNotificationsAreDisabled(true)
.init();
}

private class NotificationOpenedHandler implements OneSignal.NotificationOpenedHandler
{
@Override
public void notificationOpened(OSNotificationOpenResult result)
{
OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
String customKey;

if (data != null)
{
customKey = data.optString("customKey", null);
if (customKey != null)
Log.i("OneSignalExample", "customkey set with value: " + customKey);
}

if (actionType == OSNotificationAction.ActionType.ActionTaken)
Log.i("OneSignalExample", "Button pressed with id: " + result.action.actionID);

if (result.action.actionID.equals("posts"))
{
Intent intent = new Intent(ApplicationClass.this, SplashActivity.class);
startActivity(intent);
} else if (result.action.actionID.equals("app"))
{
String url = "http://app.brokenhearts.ml/";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
}
}
}

所以,现在的问题是,无论我点击哪个操作按钮,它都只是忽略通知。

我不知道还需要哪些其他文件。所以,这是我的 AndroidManifest.xml,以防万一:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.ananya.brokenhearts"
android:installLocation="auto" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="22" />
<uses-permission-sdk-23 android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="AllowBackup"
android:fullBackupContent="@xml/backup_descriptor"
android:name=".ApplicationClass">

<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE"/>

<activity android:name=".SplashActivity"
android:theme="@style/Splash">

<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data
android:scheme="http"
android:host="brokenhearts.ml"/>
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="https"
android:host="brokenhearts.ml"/>
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="http"
android:host="www.brokenhearts.ml"/>
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data
android:scheme="https"
android:host="www.brokenhearts.ml"/>
</intent-filter>

</activity>

<activity android:name=".MainActivity">
</activity>

</application>

</manifest>

有人可以告诉我哪里出了问题吗?我们将不胜感激。

最佳答案

为了更新这篇文章,我发布了我用来完成这项工作的代码。它可能仍然相关,也可能不相关。

package com.packagename.android;
import android.app.Application;
import android.content.Intent;
import android.util.Log;
import com.onesignal.OSNotificationAction;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;
import org.json.JSONObject;
@SuppressWarnings("unused")
public class ApplicationClass extends Application
{
@Override
public void onCreate()
{
super.onCreate();
OneSignal.startInit(this).inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification).setNotificationOpenedHandler(new NotificationOpenedHandler()).unsubscribeWhenNotificationsAreDisabled(true).init();
}
//The following part handles the notification buttons. It can trigger opening specific activities based on the additional data passed with the notification.
public class NotificationOpenedHandler implements OneSignal.NotificationOpenedHandler
{
@Override
public void notificationOpened(OSNotificationOpenResult result)
{
OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
String activityToBeOpened;
String activity;
if (data != null)
{
activityToBeOpened = data.optString("activityToBeOpened", null);
if (activityToBeOpened.equals("Update"))
{
Log.i("OneSignal", "customkey set with value: " + activityToBeOpened);
Intent intent = new Intent(ApplicationClass.this, UpdateActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
else if (activityToBeOpened.equals("Main"))
{
Intent intent = new Intent(ApplicationClass.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
}
}
}
}

澄清一下,根据您的自定义键名称替换的数据是行中的“Update”一词:if (activityToBeOpened.equals("Update")) 和 'Main'在 else if (activityToBeOpened.equals("Main")) 行 中。如果您要使用更多条件,也可以使用开关,但我不建议使用超过 2 或 3 个操作按钮。

关于java - OneSignal 操作按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51150616/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com