How can I open Main Activity if user taps on push notification sent from OpenSignal. I wanted to override the default behaviour which was causing some issue when App was active. I added following line as per the doc
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
Now if app is closed, how can i open MainActivity, and let it execute NotificationOpenedHandler.
Thank you.
If you still always want your launcher / main Activity to open / resume when tapping on a OneSignal notification add the following code to your Activity intead.
private static boolean activityStarted;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ( activityStarted
&& getIntent() != null
&& (getIntent().getFlags() & Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
finish();
return;
}
activityStarted = true;
}
See Resume last Activity when opening a Notification instructions for more details.
If you need to do something more custom keep the manifest entry you noted above and add a OneSignal NotificationOpenedHandler to OneSignal.startInit in your Application class.
import com.onesignal.OneSignal;
public class YourAppClass extends Application {
#Override
public void onCreate() {
super.onCreate();
OneSignal.startInit(this)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
.init();
}
// This fires when a notification is opened by tapping on it or one is received while the app is running.
private class ExampleNotificationOpenedHandler implements NotificationOpenedHandler {
#Override
public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
// 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
}
}
See 4. Add Optional NotificationOpenedHandler for more details on this callback.
Related
I'm trying to start MainActivity with BluetoothDevice.ACTION_ACL_CONNECTED receive just like in some stack overflow answers
public class BTReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("BT", "Receive");
String action = intent.getAction();
...
switch (action) {
case BluetoothDevice.ACTION_ACL_CONNECTED:
Intent i = new Intent();
i.setClassName("com.opendashcam", "com.opendashcam.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
And like this
Intent intent1 = new Intent(context, MainActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
But all that I can see it's just these logs (first two say that receive was gotten and activity was started with connection to written MAC)
D/BT: Receive
D/BT: started app with 00:14:41:1E:26:27
I/Timeline: Timeline: Activity_launch_request time:129879532
My Main Activity's onCreate:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("MA", "Started app");
init();
}
Since Android 10, according to Android Developers docs ,Android does not allow launching an activity from the background:
Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background. These restrictions help minimize interruptions for the user and keep the user more in control of what's shown on their screen.
As an alternative, you can show notification that will launch the activity if clicked:
In nearly all cases, apps that are in the background should display time-sensitive notifications to provide urgent information to the user instead of directly starting an activity.
I have an Unity app, that has Android plugin that can launch other applications that installed on my smartphone. Here My Java class:
public class LaunchOtherApp extends Activity
{
public static Activity mainActivity;
protected static final String LOGTAG = "MyApp";
private Activity currentActivity;
private Intent i;
private static final LaunchOtherApp ourInstance = new LaunchOtherApp();
public static LaunchOtherApp getInstance() {
return ourInstance;
}
public LaunchOtherApp(){
Log.i(LOGTAG,"Created LaunchOtherApp");
}
public void Launch( final String pack)
{
mainActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
Intent intent = new Intent();
intent.setPackage(pack);
currentActivity = UnityPlayer.currentActivity;
Context context = currentActivity.getApplicationContext();
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
Collections.sort(resolveInfos, new ResolveInfo.DisplayNameComparator(pm));
if (resolveInfos.size() > 0)
{
try
{
ResolveInfo launchable = resolveInfos.get(0);
ActivityInfo activity = launchable.activityInfo;
ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
i = new Intent(Intent.ACTION_MAIN);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.setComponent(name);
context.startActivity(i);
}
catch (SecurityException e)
{
intent = getPackageManager().getLaunchIntentForPackage(pack);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
}
}
}
});
}
#Override
public void onBackPressed() {
finish();
}
Launch - it is method that starting when I click on button in my Unity app... pack variable - it is variable that my Java method receive, and in this variable instantiate Application id. For Example I launched Youtube from my app, and when I pressing Back on my phone, I want to close Youtube, and back to my Unity app... I think I must use onBackPressed method that start when I press Back, but what I must write in this method?
finish(); doesn't help me(( Please, help... Thank you in advance!
Actually, YouTube app is a separate app (might be installed) and you cannot modify that app's behavior. On your button press, Launch method is called and assume YouTube app has started. With the intent:
If you use Intent.FLAG_ACTIVITY_NEW_TASK
YouTube app will start in a new task than yours and pressing back will not return to your app.
If you do not use Intent.FLAG_ACTIVITY_NEW_TASK
The new intent (here YouTube) will be launched in the task of calling activity (your app/game) by default and calling activity will go background. So, pressing back will again return to your app/game as you desired. Because back button press always pops from the backstack of the current task.
So, in the try block of your Launch method, removing the following line should help.
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
For more information about task and back stack, here is the official doc.
Edit
I missed that you have used application context. But, you have to launch from your calling activity context, otherwise you must use this flag. So, I have modified your Launch method and put it here, just replace your Launch method with following:
/* Method to launch an app with its package name */
public void Launch( final String pack)
{
mainActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
// Using activity context instead of application context
currentActivity = UnityPlayer.currentActivity;
Context context = currentActivity;
PackageManager pm = context.getPackageManager();
// Similar code to launch from package name
Intent intent = pm.getLaunchIntentForPackage(pack);
if (intent == null) {
// The activity cannot be found or the package name cannot be recognized
// Show some message or do something
Toast.makeText(context, "Cannot launch...", Toast.LENGTH_SHORT).show();
} else {
context.startActivity(intent);
}
}
});
}
Not sure how to get the receiver to work on the activity once the app is forced closed.
What am I missing to get this to work even if the app was forced closed? Any help would be appreciated.
I am getting the BroadcastReceiver service to work, Just not getting anything to pick up on the activity level.
I have my receiver (Service):
public class MyReceiver extends BroadcastReceiver {
public static final String SEND_NOTIFICATION_ACTION = "com.clover.sdk.app.intent.action.APP_NOTIFICATION";
#Override
public void onReceive(Context context, Intent intent) {
Log.i("MyReceiver", "Triggered MyReceiver");
String action = intent.getAction();
Bundle getIntent = intent.getExtras();
if (action.equals(SEND_NOTIFICATION_ACTION)) {
Log.i("MyReceiver Gotten", "Found");
intent = new Intent("broadCastName");
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("orderId", getIntent.getString("payload"));
Log.i("Receiver OrderID", getIntent.getString("payload"));
context.sendBroadcast(intent);
}
}
}
My Activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerReceiver(broadcastReceiver, new IntentFilter("broadCastName"));
}
}
Then my broadcastReceiver in my activity:
// Add this inside your class
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("MyReceiver Gotten 2", "Found");
Bundle b = intent.getExtras();
Log.i("MyReceiver Gotten 3", b.getString("orderId"));
new SpecificOrderAsyncTask(MainActivity.this).execute(b.getString("orderId"));
}
};
Not sure how to get the receiver to work on the activity once the app is forced closed. What am I missing to get this to work even if the app was forced closed?
That's contradictory - you can't get a receiver to work in an Activity that registered it at runtime if that Activity that is hosting the receiver is killed. When you force close, every in the app process - including the Activity and the receiver you registered with it - disappears.
The point of calling registerReceiver is to listen for broadcasts only during a specific time frame or lifecycle.
If you want the receiver to work even when the app is closed, don't register it at runtime - register it in the manifest.
Simple,
Registering service in an activity is temporary, registering service in a manifest will run even after closing the application.
But the broadcast you use is a simple message transfer system, that won't work even after you register in manifest and close the application. You have to create a background service that runs always in background in android system and should awake listening to some events passed.
i was trying the NotificationListener to get the coming notifications. Two days ago my class was an activity and everything works fine. The service started and the notification came up in my view with the title, icon and the description of it. Yesterday i implemented the new Navigation View, so, i changed the activity to fragment. After that the notification not showing up. The service starts but if i try to debug the Broadcastreceiver (that is inside the fragment) doesn't work. If i try to create some log inside it they not works. I suppose that the Broadcastreceiver not starts at this point! This is the Broadcastreceiver
/**
* Broadcast receiver notifications
***/
private BroadcastReceiver onNotice= new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String pack = intent.getStringExtra("package");
String title = intent.getStringExtra("title");
String text = intent.getStringExtra("text");
if(!pack.equals("") || !title.equals("") || !text.equals("")) {
notificationLayout.setVisibility(View.VISIBLE);
notificationTitle.setText(title);
notificationDescription.setText(text);
try {
icon = getActivity().getPackageManager().getApplicationIcon(pack);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
notificationImage.setImageDrawable(icon);
} else {
notificationLayout.setVisibility(View.INVISIBLE);
}
}
};
and in my onResume() method of the fragment i wrote this:
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(onNotice, new IntentFilter("Msg"));
I don't know if the change of the class type is the reason of this issue but it's the only change i made.
I'm android beginner so please be easy on me. I'm doing some "exercises" and i'm writing simple app which will tell RSSI strength of home wifi network. Getting that number is pretty easy, but updating it and showing that on screen it's a little more complicated as i thought.
First this is my onCreate Activity. In this activity i'm launching another android component - Service. Because the code will run in background (i know i could use thread or something else, but this is for "practice" sake, and i have a few ideas what to do with this app, while running service and not interacting with UI )
public class MainActivity extends Activity {
TextView wifi_check;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
referenceViews();
startService(new Intent(this, CheckingWifiService.class));
//wifi_check.setText(""+getIntent().getExtras().getInt("RSSI"));
}
private void referenceViews() {
wifi_check = (TextView) findViewById(R.id.wifiCheck_TV);
}
}
Because my code will run every second or so, i will use TimerTask for this purpose. And here is my TimerTask class, which includes run() method, and code for executing inside
public class TimerTsk extends TimerTask {
Context act;
WifiManager wifiMan;
WifiInfo info;
Bundle sendInfo;
Intent intent;
int rssi;
public TimerTsk(Context context) {
act = context;
}
#Override
public void run() {
intent = new Intent();
sendInfo = new Bundle();
wifiMan = (WifiManager) act.getSystemService(Activity.WIFI_SERVICE);
info = wifiMan.getConnectionInfo();
rssi = info.getRssi();
Log.d("WORKED", "RUNNING SUCESSFULLY");
// i want to send info to my activity
sendInfo.putInt("RSSI", rssi);
intent.putExtras(sendInfo);
}
}
From this class , i want to send result of RSSI to my activity and then update a text. But when i call this code below, on activity i always get NullPointerException.
wifi_check.setText(""+getIntent().getExtras().getInt("RSSI"));
To be honest i had hard time figuring out which part of code is throwing an exepction. And i found that more exactly, this part of code is throwing an exepction.
getInt("RSSI")
Overall i see that service is running, because in my LOGCAT i see a message that i create with Log.d in TimerTsk class.
Any ideas why is this happening?
Here is my service class:
public class CheckingWifiService extends Service{
int rssi;
Timer time;
TimerTsk ttsk;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
time = new Timer();
time.schedule(new TimerTsk(getApplicationContext()), 500);
return START_STICKY;
}
}
Here is my LogCat:
I see a common mistake. Don't do this:
sendInfo.putInt("RSSI", rssi);
intent.putExtras(sendInfo); // This adds a Bundle to your existing Bundle!
You are creating an Intent, with a Bundle of extras, with a Bundle that holds rssi. Leave out this unnecessary Bundle:
intent.putExtras("RSSI", rssi);
Now in your next Activity you can use:
getIntent().getIntExtra("RSSI", 0);
However you should always check to make sure there aren't any surprise null variables:
Intent in = getIntent();
if(in != null) {
int rssi = in.getIntExtra("RSSI", -1);
if(rssi < 0)
wifi_check.setText(""+rssi);
else
wifi_check.setText("Unknown");
}
is your activity starting? I don't see any call to startActivity(). In any case as mentioned by Sam you just need to call putExtra for your intent. don't forget to call
is your activity starting? I don't see any call to startActivity(). In any case as mentioned by Sam you just need to call putExtra for your intent. don't forget to call
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
you need to put this flag when start activies from background