![](/img/trans.png)
[英]How can I target a particular path/ page in an ionic app for android using onesignal push notification
[英]How to create Notification Setting page in android app using Onesignal Notification?
我想在 android 应用程序中创建一个通知设置页面,以便用户可以选择他们想要的通知类型。 我正在使用 OneSignal 推送通知服务并从 OneSignal 仪表板发送通知。
我在 Onesignal Dashboard 中创建了组/类别,该仪表板仅显示 android Oreo 及更高版本。 这就是为什么我想要在应用程序内有一个通知设置页面,以便任何版本的 android 用户都可以使用此功能。
对于从GitHub获取的名为 ExampleApplication.java 的一个信号通知,我只有一个 java 类。
package com.myapp.demo;
import android.app.Application;
import android.content.Intent;
import android.util.Log;
import com.crashlytics.android.Crashlytics;
import com.onesignal.OSNotification;
import com.onesignal.OSNotificationAction;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;
import io.fabric.sdk.android.Fabric;
import org.json.JSONObject;
public class ExampleApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Fabric.with(this, new Crashlytics());
// Logging set to help debug issues, remove before releasing your app.
OneSignal.startInit(this)
.setNotificationReceivedHandler(new ExampleNotificationReceivedHandler())
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.unsubscribeWhenNotificationsAreDisabled(true)
.init();
OneSignal.enableVibrate(false);
}
private class ExampleNotificationReceivedHandler implements OneSignal.NotificationReceivedHandler {
@Override
public void notificationReceived(OSNotification notification) {
JSONObject data = notification.payload.additionalData;
String notificationID = notification.payload.notificationID;
String customKey;
Log.i("OneSignalExample", "NotificationID received: " + notificationID);
if (data != null) {
customKey = data.optString("customkey", null);
if (customKey != null)
Log.i("OneSignalExample", "customkey set with value: " + customKey);
}
}
}
private class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
// This fires when a notification is opened by tapping on it.
@Override
public void notificationOpened(OSNotificationOpenResult result) {
OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
String customKey;
String openURL = null;
Object activityToLaunch = MainActivity.class;
if (data != null) {
customKey = data.optString("customkey", null);
openURL = data.optString("openURL", null);
if (customKey != null)
Log.i("OneSignalExample", "customkey set with value: " + customKey);
if (openURL != null)
Log.i("OneSignalExample", "openURL to webview with URL value: " + openURL);
}
if (actionType == OSNotificationAction.ActionType.ActionTaken) {
Log.i("OneSignalExample", "Button pressed with id: " + result.action.actionID);
if (result.action.actionID.equals("id1")) {
Log.i("OneSignalExample", "button id called: " + result.action.actionID);
activityToLaunch = MainActivity.class;
} else
Log.i("OneSignalExample", "button id called: " + result.action.actionID);
}
// The following can be used to open an Activity of your choice.
// Replace - getApplicationContext() - with any Android Context.
// Intent intent = new Intent(getApplicationContext(), YourActivity.class);
Intent intent = new Intent(getApplicationContext(), (Class<?>) activityToLaunch);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
// close app clearing all task
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("openURL", openURL);
Log.i("OneSignalExample", "openURL = " + openURL);
startActivity(intent);
}
}
}
我想创建这样的设置页面..
如何创建此通知设置页面?
设置.java
attendanceSwitch = findViewById(R.id.attendance_switch);
noticeSwitch = findViewById(R.id.notice_switch);
hwSwitch = findViewById(R.id.hw_switch);
testSwitch = findViewById(R.id.test_switch);
busSwitch = findViewById(R.id.bus_switch);
settingsFeedback = findViewById(R.id.settings_feedback);
if (sharedPreferences.getBoolean("animate", false)){ reduceAnimation.setChecked(true); } else { reduceAnimation.setChecked(false); }
reduceAnimation.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (compoundButton.isChecked()){
editor.putBoolean("animate" , true);
} else {
editor.putBoolean("animate" , false);
}
editor.apply();
}
});
////////////////
if (sharedPreferences.getBoolean("attendanceSwitch", true)){ attendanceSwitch.setChecked(true); } else { attendanceSwitch.setChecked(false); }
attendanceSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (compoundButton.isChecked()){
editor.putBoolean("attendanceSwitch" , true);
} else {
editor.putBoolean("attendanceSwitch" , false);
}
editor.apply();
}
});
////////////////////////
if (sharedPreferences.getBoolean("noticeSwitch", true)){ noticeSwitch.setChecked(true); } else { noticeSwitch.setChecked(false); }
noticeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (compoundButton.isChecked()){
editor.putBoolean("noticeSwitch" , true);
} else {
editor.putBoolean("noticeSwitch" , false);
}
editor.apply();
}
});
///////////////////////
if (sharedPreferences.getBoolean("hwSwitch", true)){ hwSwitch.setChecked(true); } else { hwSwitch.setChecked(false); }
hwSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (compoundButton.isChecked()){
editor.putBoolean("hwSwitch" , true);
} else {
editor.putBoolean("hwSwitch" , false);
}
editor.apply();
}
});
//////////////////////////
if (sharedPreferences.getBoolean("testSwitch", true)){ testSwitch.setChecked(true); } else { testSwitch.setChecked(false); }
testSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (compoundButton.isChecked()){
editor.putBoolean("testSwitch" , true);
} else {
editor.putBoolean("testSwitch" , false);
}
editor.apply();
}
});
//////////////////////////
if (sharedPreferences.getBoolean("busSwitch", true)){ busSwitch.setChecked(true); } else { busSwitch.setChecked(false); }
busSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (compoundButton.isChecked()){
editor.putBoolean("busSwitch" , true);
} else {
editor.putBoolean("busSwitch" , false);
}
editor.apply();
}
});
通知服务.java
if (channelid.equals("Attendance")){
if (sharedPreferences.getBoolean("attendanceSwitch", true)){
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelid);
builder.setSmallIcon(R.mipmap.ic_logo);
builder.setContentTitle(title);
builder.setContentText("Marked "+ attendance);
builder.setPriority(pri);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(NOTIFICATION_ID, builder.build());
}
} else if (channelid.equals("test")){
if (sharedPreferences.getBoolean("testSwitch", true)) {
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, PreQuiz.class), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelid);
builder.setSmallIcon(R.mipmap.ic_logo);
builder.setContentTitle(title);
builder.setContentText(body);
builder.setPriority(pri);
builder.setContentIntent(contentIntent);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(NOTIFICATION_ID, builder.build());
}
} else if (channelid.equals("notice")){
if (sharedPreferences.getBoolean("noticeSwitch", true)) {
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Noticeboard.class), PendingIntent.FLAG_UPDATE_CURRENT);
if (section.equals(sharedPreferences.getString("Section", "...")) || section.equals("All")) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelid);
builder.setSmallIcon(R.mipmap.ic_logo);
builder.setContentTitle(title);
builder.setContentText(body);
builder.setPriority(pri);
builder.setContentIntent(contentIntent);
// builder.addAction(R.drawable.ic_add_black_24dp, "Sdad", snoozePendingIntent);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(NOTIFICATION_ID, builder.build());
}
}
} else if (channelid.equals("hw")){
if (sharedPreferences.getBoolean("hwSwitch", true)) {
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Homework.class), PendingIntent.FLAG_UPDATE_CURRENT);
if (section.equals(sharedPreferences.getString("Section", "...")) || section.equals("All")) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelid);
builder.setSmallIcon(R.mipmap.ic_logo);
builder.setContentTitle(title);
builder.setContentText(body);
builder.setContentIntent(contentIntent);
builder.setPriority(pri);
// builder.addAction(R.drawable.ic_add_black_24dp, "Sdad", snoozePendingIntent);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(NOTIFICATION_ID, builder.build());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.