Reputation: 3
Receiving a notification in NotificationReceiver class that includes my data string. However, when trying to pass that string to a class in MainActivity I'm not getting any data. No errors either.
NotificationReceiver
@Override
public void onReceive(Context context, AppNotification notification) {
Toast.makeText(context, notification.payload, Toast.LENGTH_SHORT).show();
if (notification.appEvent.equals(NOTIFICATION_ACTION)) {
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("orderId", notification.payload);
context.startActivity(intent);
}
}
MainActivity
public void onNewIntent(Intent intent) {
Toast.makeText(mContext, "Hitting New Intent", Toast.LENGTH_SHORT).show();
}
protected void onHandleIntent(Intent intent) {
Toast.makeText(getApplicationContext(), "New Order Received!", Toast.LENGTH_LONG).show();
new OrderAsyncTask().execute(intent.getStringExtra("orderId"));
}
The expected result is to receive my string in either onHandleIntent or onNewIntent
UPDATED MainActivity Class Example:
public class MainActivity extends Activity {
public final static String EXTRA_PAYLOAD = "payload";
public String orderId = null;
@SuppressLint("StaticFieldLeak")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
onNewIntent(intent.getStringExtra("orderId"));
}
@Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
onNewIntent(intent.getStringExtra("orderId"));
}
public void onNewIntent(String orderId) {
if(orderId != null) {
Toast.makeText(MainActivity.this, "List order " + orderId, Toast.LENGTH_LONG);
} else {
Toast.makeText(MainActivity.this, "Value is null", Toast.LENGTH_LONG);
}
}
}
Upvotes: 0
Views: 112
Reputation: 279
The methods you are trying to get the caller intent on are not overridden from anything. I suggest you to try getting the intent and processing data on activity's onCreate method
Intent intent = getIntent();
new OrderAsyncTask().execute(intent.getStringExtra("orderId"));
Also suggest you to change
public String orderId = null;
into
public String mOrderId = null;
Or just remove it because it might get mixed up with other orderId variable on your
onHandleIntent(String orderId)
method
Edit: Since you want to pass data from notification receiver to your mainActivity while the activity is running, I would suggest you to register the receiver inside your MainActivity
Please see this link, example of such design with Clover SDK https://github.com/clover/clover-android-sdk/blob/master/clover-android-sdk-examples/src/main/java/com/clover/android/sdk/examples/AppNotificationTestActivity.java
From the code:
private String orderId;
private AppNotificationReceiver receiver = new
AppNotificationReceiver() {
@Override
public void onReceive(Context context, AppNotification notification) {
log("Received " + notification);
orderId = notification.payload();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Register the receiver to your mainActivity
receiver.register(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
receiver.unregister();
}
You can directly get orderId into your MainActivity like this
Upvotes: 1