2017-03-25 3 views
1

私は通知を通知するためのシグナルライブラリを実装していますが、私はアプリが実行されていない間にプッシュ通知のクリックからペトリキュラのアクティビティを開きたいですよ。 私はpushnotificatonを受け取っていますが、アプリがクラッシュするここでの通知受信のための私のコードはOneSignal Push Notification actiivtyを開くにはここをクリック

パブリッククラスExampleNotificationOpenedHandlerは{

Context context; 
@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.e("OneSignalExample", "customkey set with value: " + customKey); 

    } 

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

    Intent intent = new Intent(context, User_Detail.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(intent); 


} 

OneSignal.NotificationOpenedHandlerを実装しているここに私のエラーMSGが

Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference 

答えて

2

に私はちょうどクラスのコンストラクタを構築するために逃しました。またonReceivedMethod

Context context2; 

ExampleNotificationOpenedHandler(Context context){ 
    context2 = context; 
} 

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

    Intent intent = new Intent(context2,User_Detail.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); 
    context2.startActivity(intent); 


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

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


    } 

とは、Applicationクラスのコンテンツを渡す前にも

@Override 
public void onCreate() { 
    super.onCreate(); 
    mInstance = this; 


    OneSignal.startInit(this) 
      .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler(this)) 
      .init(); 

} 
0

ですこれはcontext変数がnullであることを意味します。あなたが選んだconstrucor

public Intent(Context packageContext, Class<?> cls) { 
    mComponent = new ComponentName(packageContext, cls); 
} 

そしてComponentName construcor

public ComponentName(Context pkg, String cls) { 
    if (cls == null) throw new NullPointerException("class name is null"); 
    mPackage = pkg.getPackageName(); //here you get crash 
    mClass = cls; 
} 

でテントのソースコードを見ると

あなたは、たとえばDagger2のために、コンテキストを提供するために、DIを使用することができます。

またはアプリケーションクラスを実装

public class DemoApp extends Application { 

    private static DemoApp instance; 

    @Override 
    public void onCreate() { 
     instance = this; 
     super.onCreate(); 
    } 

    public static DemoApp getInstance() { 
     return instance; 
    } 

} 

そして、あなたのマニフェスト

<application 
     android:name=".demo.DemoApp" 
+0

おかげで私はコンストラクタでコンテキストを渡すために逃したバディ。 –