我尝试与OneSignal建立聊天应用。我希望listview在通知到来时设置适配器。如果我使用setNotificationOpenedHandler
它的工作,但我应该点击提示通知,使listview设置适配器。问题是,我不想点击提示/通知来设置适配器列表视图,所以我使用setNotificationReceivedHandler
,但不工作,不改变任何东西。
这适用于Open Handler(工作)
OneSignal.startInit(this)
.setNotificationOpenedHandler(new OneSignal.NotificationOpenedHandler() {
@Override
public void notificationOpened(OSNotificationOpenResult result) {
ArrayList<HashMap<String, String>> dt = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("a", "testing 2");
dt.add(map);
ChatMessageAdapter adap = new ChatMessageAdapter(ChatActivity.this, dt);
listView = (ListView) findViewById(R.id.listViewChat);
listView.setAdapter(adap);
}
})
.init();
这是接收处理程序(不工作)
OneSignal.startInit(this)
.setNotificationReceivedHandler(new OneSignal.NotificationReceivedHandler() {
@Override
public void notificationReceived(OSNotification notification) {
ArrayList<HashMap<String, String>> dt = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("a", "testing 1");
dt.add(map);
ChatMessageAdapter adap = new ChatMessageAdapter(ChatActivity.this, dt);
listView = (ListView) findViewById(R.id.listViewChat);
listView.setAdapter(adap);
}
})
.init();
答案 0 :(得分:1)
试试这个:
在应用程序代码中使用此代码:
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().build());
OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.NONE);
OneSignal.startInit(this)
.autoPromptLocation(true)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
.setNotificationReceivedHandler(new ExampleNotificationReceivedHandler())
.init();
这是 ExampleNotificationReceivedHandler :
public class ExampleNotificationReceivedHandler implements NotificationReceivedHandler {
/**
* Callback to implement in your app to handle when a notification is
* received while your app running in the foreground or background.
*
* Use a NotificationExtenderService instead to receive an event even when
* your app is closed (not 'forced stopped') or to override notification
* properties.
*
* @param notification
* Contains information about the notification received.
*/
@Override
public void notificationReceived(OSNotification notification) {
Log.w("OneSignalExample", "notificationReceived!!!!!!");
DebuggingHelper.printObject(notification);
DebuggingHelper.printObject(notification.payload);
}
}
这是 ExampleNotificationOpenedHandler :
public class ExampleNotificationOpenedHandler implements NotificationOpenedHandler {
@Override
public void notificationOpened(OSNotificationOpenResult openedResult) {
OSNotificationAction.ActionType actionType = openedResult.action.actionType;
JSONObject data = openedResult.notification.payload.additionalData;
String customKey = data.optString("customkey", null);
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: " + openedResult.action.actionID);
// The following can be used to open an Activity of your choice.
/*
* Intent intent = new Intent(getApplication(), YourActivity.class);
* intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |
* Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);
*/
// Follow the instructions in the link below to prevent the launcher
// Activity from starting.
// https://documentation.onesignal.com/docs/android-notification-customizations#changing-the-open-action-of-a-notification
}
}
我希望这对你有帮助。
答案 1 :(得分:1)
使用Logback for Android记录OneSignal通知的提示:
请参阅上面的Iman以及此链接:https://github.com/OneSignal/OneSignal-Android-SDK/blob/master/OneSignalSDK/app/src/main/java/com/onesignal/example/OneSignalExampleApp.java
当为ASYNC配置了Logback for Android时,我无法在处理程序中记录任何OneSignal事件
使用此或类似的logback.xml
<configuration debug="true">
<!-- Create a tab delimited file appender for a log in the application's data directory -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DATA_DIR}/logs/${PACKAGE_NAME}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${DATA_DIR}/logs/${PACKAGE_NAME}.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>3</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>100KB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{yy.MM.dd HH:mm:ss.SSS}:\t[%thread]:\t%level:\t%logger: \t%msg%n</pattern>
<outputPatternAsHeader>true</outputPatternAsHeader>
<immediateFlush>true</immediateFlush>
</encoder>
</appender>
<!--
<appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="FILE" />
</appender>
-->
<!-- Create a logcat appender -->
<appender name="logcat" class="ch.qos.logback.classic.android.LogcatAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS yy.MM.dd}\t[%thread]\t%-5level\t%logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="ALL">
<appender-ref ref="FILE" />
<appender-ref ref="logcat" />
</root>
<!--
<root level="ALL">
<appender-ref ref="ASYNC" />
<appender-ref ref="logcat" />
</root>
-->
</configuration>
package org.MyAwesomeApp.onesignalTest.app;
import android.app.Application;
import android.os.StrictMode;
import com.onesignal.*;
import com.onesignal.OneSignal.NotificationOpenedHandler;
import org.slf4j.*;
public class pkApplication extends Application {
//https://rtyley.github.io/spongycastle/
/*
static private final Provider SpongyCastleProvider = new org.spongycastle.jce.provider.BouncyCastleProvider();
static { java.security.Security.insertProviderAt(SpongyCastleProvider, 1); }
*/
static private final Logger mLog = LoggerFactory.getLogger( pkApplication.class );
@Override
public void onCreate() {
super.onCreate();
//mLog.debug("pkApplication.OnCreate:\t name: " + SpongyCastleProvider.getName() + "\t info: " + SpongyCastleProvider.getInfo());
initOneSignal();
}
void initOneSignal(){
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().build() );
/*
OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.NONE);
*/
OneSignal.startInit(this)
// .autoPromptLocation(true)
// .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.setNotificationOpenedHandler( new pkNotificationOpenedHandler() )
.setNotificationReceivedHandler( new pkNotificationReceivedHandler() )
.init();
OneSignal.sendTag("test1", "test1JD");
}//initOneSignal
// This fires when a notification is opened by tapping on it or one is received while the app is runnning.
private class pkNotificationOpenedHandler implements NotificationOpenedHandler{
@Override public void notificationOpened(OSNotificationOpenResult openedResult ){
OSNotification notification = openedResult.notification;
mLog.debug( "notification Opened:\t" + notification.toJSONObject() );
}//n otificationOpened
}//pkNotificationOpenedHandler
private class pkNotificationReceivedHandler implements OneSignal.NotificationReceivedHandler{
@Override public void notificationReceived( OSNotification notification ){
mLog.debug( "notification received:\t" + notification.toJSONObject() );
}
}//pkNotificationReceivedHandler
}//pkApplication