2016-08-08 69 views
0

我尝试使用OneSignal制作聊天应用程序。而且我希望listview在通知即将到来时设置适配器。如果我使用setNotificationOpenedHandler它的工作,但我应该点击提示通知,使列表视图设置适配器。问题是,我不希望点击提示/通知来设置适配器listview,所以我使用setNotificationReceivedHandler,但不工作,没有改变任何东西。OneSignal setNotificationReceivedHandler无法设置适配器

这种开放式处理器(工作

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(); 
+1

我无法重现任何问题'notificationReceived'不射击。确保你从Application类的'onCreate'调用'OneSIgnal.startInit(..)'而不是一个Activity。 – jkasten

回答

1

试试这个:

  1. 下载最新的OneSignal Android SDK
  2. 使用此代码在应用程序代码:

    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(); 
    
  3. 这是你的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); 
    } 
    } 
    
  4. 这是你的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 
        } 
        } 
    

    我希望这可以帮助你。

+0

谢谢你,Iman Marashi,我会试试Tomorow! – kresek

1

提示在登录使用的logback 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 
+0

非常感谢! – kresek

+0

如果你喜欢它,请投票“回答” – JDOaktown