I am attempting to migrate an app from Parse to OneSignal. Things have been running fairly smooth except for one issue. If the app has not already been started, clicking (opening) the notification does not launch the app and bring it to the foreground.
I am using a custom onNotificationOpenedHandler:
Main Activity, onCreate
// Init OneSignal
OneSignal.startInit(this).setNotificationOpenedHandler(new NotificationOpenHandler()).init();
NotificationOpenedHandler, also in the launcher Main Activity:
class NotificationOpenHandler implements OneSignal.NotificationOpenedHandler {
// This fires when a notification is opened by tapping on it.
#Override
public void notificationOpened(OSNotificationOpenResult result) {
JSONObject data = result.notification.payload.additionalData;
String stationName = data.optString("stationName");
String timestamp = data.optString("timestamp");
String filename = data.optString("filename");
String url = getString(R.string.callResourceUrl) + filename;
Log.d("APP", "Notification clicked");
Intent intent = new Intent(getApplicationContext(), CallActivity.class);
intent.putExtra("stationName", stationName);
intent.putExtra("time", timestamp);
intent.putExtra("url", url);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
Here is my Android Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.opheliadesign.tevfd">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:supportsRtl="true"
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:logo="#drawable/ic_launcher"
android:launchMode="singleInstance"
android:theme="#style/Theme.Tevfd">
<activity android:name="com.example.app.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".CallActivity"
android:label="#string/title_activity_call"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app.MainActivity" />
</activity>
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
</application>
</manifest>
Any ideas?
UPDATE
Previously, using Parse, I utilized a BroadcastReceiver with a PendingIntent. It seems a bit unclear how to accomplish this with OneSignal as so much seems to be configured automatically.
If I remove the custom Intent, the application comes to the foreground.
Any help would be greatly appreciated.
Related
I want to have a secondary activity which will be a map activity that can be opened by a button click on the main menu of an app. So I am using the built in map activity in android studio and when I try and launch that alone the map works. But when I try to add a main home page with button and then use that button to launch the map it doesn't work and my app closes. Does anyone know the correct way to launch a normal map from a button. I have all the implementations in the gradle files correct. I will include my manifest and main activity below.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_fragment1,container,false);
bt = v.findViewById(R.id.launchmap);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), MapsActivity.class);
startActivity(intent);
}
});
return v;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package = "com.example.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="package_name.permission.MAPS_RECEIVE"/>
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.MyApplication"
tools:targetApi="31" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".MapsActivity"
android:exported="true">
<intent-filter>
<category android:name="android.intent.category.default.DEFAULT" />
</intent-filter>
</activity>
<meta-data
android:name="come.google.android.geo.API_KEY"
android:value="AIzaSyBgSEI9Xvh3TicLaNj8FigA4iRyP1jdEVs" />
<activity
android:name=".SecondActivity"
android:exported="true" >
</activity>
<activity
android:name=".MainActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Follow these steps
https://developers.google.com/maps/documentation/android-sdk/start
#Android_map
I am writing an application running on my custom device (marshmallow based). the general idea is to prompt a user dialog to enter some information at boot and send it to my server. once the information is successfully sent I update the shared preferences that the information was sent and the dialog will not be prompt anymore.
as always I check to see if the needed permissions are present (read phone state for IMEI and sim info), if not I create an activity that requests permissions and on result starts the user dialog activity. if permissions already exist I create the dialog activity straight from the boot receiver.
however, for some reason, sometimes the permissions activity is created twice (I made sure the boot receiver is only triggered once). this causes as expected the user dialog to be shown twice for no reason. does anyone know how can I solve this? thank you.
here is a code snippet for the boot receiver (the only place where the permissions activity is started).
public class BootReceiver extends BroadcastReceiver {
SharedPreferences mPrefs;
boolean wasUpdated;
#Override
public void onReceive(Context context, Intent intent) {
Log.v("car_num", "got boot receiver");
mPrefs = context.getSharedPreferences("car_num", MODE_PRIVATE);
wasUpdated = mPrefs.getBoolean("is_num_updated", false);
if (!wasUpdated) {
Log.v("car_num", "first time - asking for car num");
if (UtilityFunctions.checkPermission(context, Manifest.permission.READ_PHONE_STATE)) {
Log.v("car_num", "we have permission lets ask for car num");
Intent i = new Intent();
i.setClassName("com.mgroup.carnumberapp", "com.mgroup.carnumberapp.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Log.v("car_num", "main activity started");
} else {
Log.v("car_num", "no permission - so requesting");
Intent i = new Intent();
i.setClassName("com.mgroup.carnumberapp", "com.mgroup.carnumberapp.PermissionActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("from", "boot receiver");
context.startActivity(i);
}
} else {
Log.v("car_num", "car number already updated - doing nothing");
}
}
}
edit:
I tried adding
android:launchMode = "singleTop"
however, it happened again.
here is my manifest :
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/MGServiceTheme">
<service
android:name=".SendDetailsService"
android:enabled="true"
android:exported="true"></service>
<activity android:name=".PermissionActivity"
android:launchMode = "singleTop"/>
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action
android:name="android.intent.action.MAIN"
android:theme="#style/Theme.AppCompat.NoActionBar" />
</intent-filter>
</activity>
</application>
In your manifest do android:launchMode="singleTop" for an activity
like
<activity android:name=".MapActivity"
android:launchMode="singleTop"/>
<activity android:name=".MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I'm writing an Android FTP server program. I need to select files to send to client, so I need a file chooser and I wrote this code:
Java:
Intent filechooser= new Intent(Intent.ACTION_GET_CONTENT);
filechooser.addCategory("*/*");
filechooser.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(filechooser, 10);
XML:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sender">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Chooser"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I get a No activity found to handle intent exception.
You are using wrong file category(/). It should be a type instead of category.
If you want to choose any file from storage you need code like below.
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
}
Source: here
First of all:
ALL of the answers of the related questions did not solved my problem, thats why i am creating a new question.
Explanation:
I have a huge android app, i use Eclipse-ADT plugin and i am using a smartphone that have android 4.0.4 to do the tests, i have a lot of Activity's and i pass values between Activity's via Bundle getExtras in onCreate() and too i get some values on onActivityResult's.
Obs:
(1) = Activity A that calls for result -> Activity B
(2) = Activity B that get values from Activity A and returns other values to -> Activity A.
Problem:
I have ProdutosForm.java(1) activity and PedidoItemx.java(2) activity.
Inside ProdutosForm.java(1), i am starting PedidoItemx.java(2) activity to get a result:
Code for item 2:
<!-- language: lang-java -->
Intent i = new Intent();
i.setClass(this, PedidoItemx.class);
i.putExtra("CODE", code );
...
... (a LOT of putExtras....)
...
//and after putExtras, thats the relevant part
startActivityForResult(i, 1); //i its the Intent setted as PedidoItemx.class
Inside PedidoItemx.java(2) onCreate method i have:
Code for item 3:
<!-- language: lang-java -->
Bundle extras = getIntent().getExtras();
code = extras.getString("CODE");
4. Too, still inside PedidoItemx.java(2) i set some new values and i set the result to RESULT_OK:
Code for item 4
<!-- language: lang-java -->
Intent i = getIntent();
i.putExtra("PEDIDO", cPedPedido);
i.putExtra("VLRFRETE", valorFrete);
setResult(RESULT_OK, i); //RESULT_OK is a native constant that have value -1
finish();
Now, returning to ProdutosForm.java(1) i have the following onActivityResult code to get the values returned by PedidoItemx.java(2):
Code for item 5:
<!-- language: lang-java -->
public void onActivityResult(int requestCode,int resultCode,Intent data){
if(resultCode==RESULT_OK && requestCode==1){
Bundle MBuddle = data.getExtras();
Intent i = getIntent();
String cPedidoExtra = MBuddle.getString("PEDIDO");
i.putExtra("PEDIDO", cPedidoExtra);
i.putExtra("VLRFRETE", MBuddle.getFloat("VLRFRETE", 0));
cPedPedido = cPedidoExtra; //this does nothing important
setResult(RESULT_OK, i);
}
}
Everything looks OK and should work, but it doesn't. BECAUSE my requestCode is always 0 and my data is always null thats the real problem.
My manifest:
<!-- language: lang-xml -->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bee.mobile"
android:versionCode="2"
android:versionName="1.50" >
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="7" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCOUNT_MANAGER" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<!-- <uses-permission android:name="android.permission.RECEIVE_SMS" /> -->
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:screenOrientation="user"
android:debuggable="true"> <!-- mudar para false ao gerar depois -->
<activity
android:name="bee.mobile.form.outros.SplashActivity"
android:label="#string/app_name"
android:screenOrientation="sensor">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Outros -->
<activity android:name="bee.mobile.form.outros.MainActivity"/>
<activity android:name="bee.mobile.form.outros.HistoricoClienteForm"/>
<activity android:name="bee.mobile.form.outros.HistoricoListaNotasForm"/>
<activity android:name="bee.mobile.form.outros.HistoricoNotaForm"/>
<activity android:name="bee.mobile.form.outros.HistoricoProdutoForm"/>
<activity android:name="bee.mobile.form.outros.HistoricoMesProdForm"/>
<activity android:name="bee.mobile.form.outros.HistoricoQuemForm"/>
<activity android:name="bee.mobile.form.outros.PedidoItemx"/>
<activity android:name="bee.mobile.form.outros.RelatoriosForm"/>
<activity android:name="bee.mobile.form.outros.EstufaDetalhes"
android:windowSoftInputMode="stateHidden"/>
<!-- Lista -->
<activity android:name="bee.mobile.form.lista.ClientesForm"/>
<activity android:name="bee.mobile.form.lista.ProdutosForm"/>
<activity android:name="bee.mobile.form.lista.PedidoListaForm"/>
<activity android:name="bee.mobile.form.lista.ContatosListaForm"/>
<activity android:name="bee.mobile.form.lista.GruposForm"/>
<activity android:name="bee.mobile.form.lista.NotaListaForm"/>
<!-- Cadastro -->
<activity android:name="bee.mobile.form.cadastro.PedidoCadForm"/>
<activity android:name="bee.mobile.form.cadastro.ClientesCadastroForm"
android:windowSoftInputMode="stateHidden"/>
<activity android:name="bee.mobile.form.cadastro.ContatosCadForm"
android:windowSoftInputMode="stateHidden"/>
<activity android:name="bee.mobile.form.cadastro.PedidoCadForm_Financeiro"/>
<!-- Sem destino -->
<activity android:name="bee.mobile.ConfigurarForm"/>
<activity android:name="bee.mobile.ConfiguracaoLocalForm"/>
<activity android:name="bee.mobile.AtualizaManual"/>
<activity android:name="bee.mobile.ReceberForm"/>
<activity android:name="bee.mobile.ResolucaoForm"/>
<activity android:name="bee.mobile.AtualizarForm"/>
<activity android:name="bee.mobile.ParticipantesVendaForm"/>
</application>
</manifest>
Question:
Why i cant get the data returned from Activity B?? i am doing something wrong?
I figured out the solution for my problem:
I dont know WHY but if i MOVE this piece of code that is actually on buttonClick event on Activity B(2):
Intent i = getIntent();
i.putExtra("PEDIDO", cPedPedido);
i.putExtra("VLRFRETE", valorFrete);
setResult(RESULT_OK, i); //RESULT_OK is a native constant that have value -1
finish();
To -> right after onCreate, it works ok.
But if i move back this code "to the right place" on a button click, it doesnt work, i really dont know why the activity is having this very weird behaviour, but i'll be happy to share when i figure it out.
In other words, i have to set the extras and the RESULT_OK before doing anything, otherwise it didnt work.
I think you should add an action ACTION_SEND to your intent to return to the other activity. Something like this:
Intent i = getIntent();
**i.setAction(Intent.ACTION_SEND);**
i.putExtra("PEDIDO", cPedPedido);
i.putExtra("VLRFRETE", valorFrete);
setResult(RESULT_OK, i);
finish();
Use:
Intent i = this.getIntent();
instead of
Intent i = getIntent();
in the activity where you need to receive data of the intent, which has the values binded to some keys.
I have literally followed the examples for implementing GCM inside an android app and the app is receiving the messages using the following code:
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp;
comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
The problem is that the GcmIntentService (public class GcmIntentService extends IntentService) is never being run? Am I missing something or should it usually fire the onHandleIntent method?
In case in any way related the Manifest file is below, thanks in advance:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.diversivi.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19"/>
<application
android:label="#string/app_name"
android:icon="#drawable/ic_launcher">
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
<activity
android:name=".DemoActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.diversivi.test" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.diversivi.test.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.diversivi.test.permission.C2D_MESSAGE" />
</manifest>
I don't see your GcmIntentService service declared in the manifest. That could be the problem.
In addition, the following line
comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());
assumes that the GcmIntentService class is in the main package of your app (i.e. its full name is com.diversivi.test.GcmIntentService). If that's not the case, the broadcast receiver can't find the service class.