Splashscreen and Intents - java

I'm developing this android application, that receives data from the gallery (in my case an image) and displays it. What i also had to do is to create a SplashScreen for my app. But when i did my intent became null .
Manifest code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xkbc1923.myapplication">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_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=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AlertExampleActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
<data android:mimeType="text/*" />
</intent-filter>
</activity>
</application>
SplashScreen
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashScreen extends Activity {
private static final int DISPLAY_DURATION = 1000;
#Override
protected final void onCreate(final Bundle savedInstState) {
super.onCreate(savedInstState);
setContentView(R.layout.activity_splashscreen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreen.this, AlertExampleActivity.class);
startActivity(i);
// close this activity
finish();
}
}, DISPLAY_DURATION);
}
}
MainActivity
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import static android.provider.CalendarContract.CalendarCache.URI;
public class AlertExampleActivity extends AppCompatActivity {
ImageView picView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
///get the image view
//get the text view
setContentView(R.layout.activity_alert_example);
picView = (ImageView) findViewById(R.id.picture);
TextView txtView = (TextView) findViewById(R.id.txt);
if (txtView ==null) {
Log.w("Example", "TextView is null");
}
//get the received intent
Intent receivedIntent = getIntent();
//get the action
String receivedAction = receivedIntent.getAction();
//find out what we are dealing with
String receivedType = receivedIntent.getType();
//make sure it's an action and type we can handle
if (receivedAction.equals(Intent.ACTION_SEND)) {
Log.d("Example", "Send received");
//content is being shared
if (receivedType.startsWith("text/")) {
Log.d("Example", "Text received");
//handle sent text
//hide the other ui item
picView.setVisibility(View.GONE);
//get the received text
String receivedText = receivedIntent.getStringExtra(Intent.EXTRA_TEXT);
//check we have a string
if (receivedText != null) {
//set the text
txtView.setText(receivedText);
}
} else if (receivedType.startsWith("image/")) {
Log.d("Example", "Image received");
//handle sent image
handleSendImage(receivedIntent);
}
} else if (receivedAction.equals(Intent.ACTION_MAIN)) {
//app has been launched directly, not from share list
Log.d("Example", "Direct launch of App");
}
}
private void handleSendImage(Intent intent) {
// Get the image URI from intent
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
// When image URI is not null
if (imageUri != null) {
// Update UI to reflect image being shared
picView.setImageURI(imageUri);
} else{
Toast.makeText(this, "Error occured, URI is invalid", Toast.LENGTH_LONG).show();
}
}
}

You did not add any extra data to your intent. So it would be null normally. Just add necessary data to Intent (i) on SplashScreen.java. Ex -
i.setAction("action");
i.setType("type");
i.putExtra("key", "value");

I am not sure if this gonna solve your problem but you can try.
Instead of :
Intent i = new Intent(SplashScreen.this, AlertExampleActivity.class);
startActivity(i);
// close this activity
finish();
Try this:
startActivity(new Intent(SplashScreen.this, AlertExampleActivity.class));
finish();

I've changed the manifest file since i want the splashscreen to appear even when it's externally called .
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_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=".AlertExampleActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
<data android:mimeType="text/*" />
</intent-filter>
</activity>
</application>
And changed the SplashscreenActivity :
public class SplashScreen extends Activity {
private static final int DISPLAY_DURATION = 1000;
#Override
protected final void onCreate(final Bundle savedInstState) {
super.onCreate(savedInstState);
setContentView(R.layout.activity_splashscreen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreen.this, AlertExampleActivity.class);
i.setAction(Intent.ACTION_SEND);
i.setType("*/*");
String[] mimetypes = {"image/*", "video/*"};
i.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivity(i);
// close this activity
finish();
}
}, DISPLAY_DURATION);
}
}
I haven't changed anything in the MainActivity , but now i no longer receive the image ... I'm new to Android so i would really appreciate the explanation.

Related

why is my sms reciever code not responding

I'm a newbie to android programming
I am trying to read incoming SMS and the SMS sender number and display it in my app.
As soon as my phone receives an SMS the app closes
Is there a simple Solution, Should I not be using two classes in the same activity
package com.example.readnewmsg;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.provider.Telephony;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsMessage;
import com.example.readnewmsg.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
}
public class SmsListner extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) {
String smsSender = "";
String smsBody = "";
for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
smsBody += smsMessage.getMessageBody();
smsSender += smsMessage.getOriginatingAddress();
binding.MsgText.setText(smsBody);
binding.NoView.setText(smsSender);
}
}
}
}
}
I have Added these permissions in My manifest file
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
And also these intent filters
<application
android:allowBackup="true"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MainActivity$SmsListner">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>

App Service not starting when phone boots up

I have made a simple service in android studio which should start as soon as I boot the phone. It should display atleast a TOAST Message. I am using Redmi note 4 as emulator and the service is not starting when I boot or reboot the phone. I have set the app to autostart also in settings.
Android Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dilip3.myapplication" >
<!-- Permission for starting app on boot -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<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=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Service required starting app on boot -->
<service android:name=".MyService" android:label="My Service">
<intent-filter>
<action android:name="com.myapp.MyService" />
</intent-filter>
</service>
<receiver
android:enabled="true"
android:name=".BootService"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.REBOOT"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
</application>
</manifest>
BootService.java
package com.example.dilip3.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class BootService extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Toast.makeText(context, "Boot Completed", Toast.LENGTH_SHORT).show();
Intent serviceIntent = new Intent(context, MyService.class);
context.startActivity(serviceIntent);
}
}
}
MyService.java
package com.example.dilip3.myapplication;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;
public class MyService extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_LONG).show();
return super.onStartCommand(intent,flags,startId);
}
}
MainActivity.java has no changes.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
You need to start a service, not an activity.
From
context.startActivity(serviceIntent);
To
context.startService(serviceIntent);

how to customize parse push notification to send uri in android application?

I want to use parse push notification feature in my android application.
I put my parse push notification code in my first activity (splash activity).
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import com.parse.ParseAnalytics;
import com.parse.ParseInstallation;
import com.parse.PushService;
public class SplashActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
ParseAnalytics.trackAppOpened(getIntent());
// inform the Parse Cloud that it is ready for notifications
PushService.setDefaultPushCallback(this, HomeActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashActivity.this, HomeActivity.class);
startActivity(intent);
SplashActivity.this.finish();
}
}, 1000);
}
public void onBackPressed() {
}
}
and here is my ParseApplication :
import com.parse.Parse;
import com.parse.ParseACL;
import com.parse.ParseUser;
import android.app.Application;
public class ParseApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
// Add your initialization code here
Parse.initialize(this, " id", "id");
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
// If you would like all objects to be private by default, remove this line.
defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
}
}
and here is my manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:name="ParseApplication"
android:icon="#drawable/icon"
android:label="#string/app_name" >
<activity
android:name=".SplashActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HomeActivity" ></activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
</application>
now I can easily send push notification in application with json like this:
{"title" : "my title",
"alert" : " my alert text",
"uri" :"http://Google.com"
}
here I can easily get my title & my alert but the problem is that it doesn't my uri at all when I click on it and it goes to my home activity.
what's my problem? I want to open my uri.
You need to add in the URI field the name of an activity.
For example:
.SplashActivity
In my case we have this Manifest:
...
<activity
android:name=".presenter.FooActivity"
android:label="#string/title_activity_foo"
android:parentActivityName=".presenter.fooParent">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="myapp" android:host="host" android:path="/path" />
</intent-filter>
</activity>
...
And the json for the Push using Parse is:
{"title" : "my title",
"alert" : " my alert text",
"uri" :"myapp://host/path"
}
If you want to open http://www.google.com you may need to have some code receeiving the Intent on your Activity and creating a new one to go to the URL. For that you will need to add a custom attribute for your json to pass the url you need. Something like:
{"title" : "my title",
"alert" : " my alert text",
"uri" :"myapp://host/path",
"customurl":"http://www.google.com"
}
and the code:
Bundle params = intent.getExtras();
if (params!=null)
{
String param = params.getString("com.parse.Data");
String url = funcThatGetsUrlFromData(param);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}

How can I send and retrieve data with parse.com

I wrote that code to send data to parse.com then to receive it also but I can't even send it till now, why ?!! I don't know the main reason, I put the internet and network permission in AndroidManifest.xml the parse class in application also in AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mkadaimtwo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name="com.example.mkadaimtwo.ParseCode"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
and also put the parse class in application also in AndroidManifest.xml and that two codes of class and main Activity
ParseCode class Activity :
package com.example.mkadaimtwo;
import com.parse.Parse;
import android.app.Application;
public class ParseCode extends Application {
public void onCreate() {
Parse.initialize(this, "GIuhlGILKRd8itvCF79femTyReHM6XjVkrfLKm3X", "Fjg4tBrMgl0mY47K4kCL7hVmXhu8FmkE2on9PlXK");
}
}
the MainActivity Code :
package com.example.mkadaimtwo;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.net.ParseException;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;
import com.parse.GetCallback;
import com.parse.ParseObject;
import com.parse.ParseQuery;
public class MainActivity extends ActionBarActivity {
EditText etCompanyName,etAddress,etNumberOfEmployees,etContactNumber;
ProgressDialog pd,pd2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etCompanyName = (EditText) findViewById(R.id.etCompanyName);
etAddress = (EditText) findViewById(R.id.etAddress);
etNumberOfEmployees = (EditText) findViewById(R.id.etNumberOfEmployees);
etContactNumber = (EditText) findViewById(R.id.etContactNumber);
pd = new ProgressDialog(this);
pd.setTitle("wait");
pd.setMessage("by7aml elmafrod");
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setCancelable(true);
pd2 = new ProgressDialog(this);
pd2.setTitle("wait");
pd2.setMessage("by7aml elmafrod");
pd2.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd2.setCancelable(true);
//here we will Load the old company data from Parse.com
ParseQuery<ParseObject> query = ParseQuery.getQuery("TestBosyApp");
query.getInBackground("ijy1qi78g8", new GetCallback<ParseObject>() {
public void done(ParseObject testBosyApp, ParseException e) {
if (e == null) {
String companyName = testBosyApp.getString("company_name");
String address = testBosyApp.getString("address");
String numberOfEmployees = testBosyApp.getString("number_of_employees");
String contactNumber = testBosyApp.getString("contact_number");
etCompanyName.setText(companyName);
etAddress.setText(address);
etNumberOfEmployees.setText(numberOfEmployees);
etContactNumber.setText(contactNumber);
pd.dismiss();
} else {
AlertDialog.Builder mDialoge = new AlertDialog.Builder(MainActivity.this);
mDialoge.setTitle("Erorr");
mDialoge.setMessage("Check el net plz :)");
mDialoge.setPositiveButton("ok", null);
mDialoge.show();
}
}
#Override
public void done(ParseObject arg0, com.parse.ParseException arg1) {
// TODO Auto-generated method stub
}
});
}
public void update (View V){
pd2.show();
//update data in Parse
ParseQuery<ParseObject> myQuery = ParseQuery.getQuery("TestBosyApp");
// Retrieve the object by id
myQuery.getInBackground("U6Gwn2tiD8", new GetCallback<ParseObject>() {
public void done(ParseObject testBosyApp, ParseException e) {
if (e == null) {
//Initials our variables
String companyName = etCompanyName.getText().toString().trim();
String address = etAddress.getText().toString().trim();
String numberOfEmployees = etNumberOfEmployees.getText().toString().trim();
String contactNumber = etContactNumber.getText().toString().trim();
//update it with new data
testBosyApp.put("company_name", companyName);
testBosyApp.put("address", address);
testBosyApp.put("number_of_employees", numberOfEmployees);
testBosyApp.put("contact_number", contactNumber);
testBosyApp.saveInBackground();
pd2.dismiss();
AlertDialog.Builder mDialoge = new AlertDialog.Builder(MainActivity.this);
mDialoge.setTitle("2shta");
mDialoge.setMessage("Keda eldata ra7t t2riban");
mDialoge.setPositiveButton("cool", null);
mDialoge.show();
}else{
pd2.dismiss();
AlertDialog.Builder mDialoge = new AlertDialog.Builder(MainActivity.this);
mDialoge.setTitle("Erorr");
mDialoge.setMessage("Check el net plz :)");
mDialoge.setPositiveButton("ok", null);
mDialoge.show();
}
}
#Override
public void done(ParseObject arg0, com.parse.ParseException arg1) {
// TODO Auto-generated method stub
}
});
}
}
so what's the problem, progress-bar is loading without end and if I give progress-bar cancellation feature then put data in fields there's nothing happen .
plus if anyone have tutorials for use android with parse.com please provide me with it.
Dude.. Sir? goto parse.com, there are tutorials.. you need to enable something i just do not remember Go to settings ->App Permissions ->Allow client class creation. Set it to ON> before you can send push from device and receive it.. also your manifest is not complete.. from what i know..
<application
android:name="com.example.mkadaimtwo.ParseCode"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//edit started here
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
</application>
so copy and paste this manifest

Parse Push "No Activity Found" on opening Push Notification

I made an app using Parse as backend. I am able to get the notification but when i tap on it, the app crashes.
Here are the java and XML files.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="chipset.lugmnotifier">
<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" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<permission
android:name="chipset.lugmnotifier.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="chipset.lugmnotifier.permission.C2D_MESSAGE" />
<application
android:name=".resources.ParseInitApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<meta-data
android:name="com.parse.push.notification_icon"
android:resource="#drawable/ic_notification" />
<activity
android:name=".HomeActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AdminActivity"
android:label="#string/title_activity_admin"
android:parentActivityName=".HomeActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".HomeActivity" />
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="chipset.lugmnotifier" />
</intent-filter>
</receiver>
</application>
</manifest>
ParseInitApplication.java
import android.app.Application;
import android.util.Log;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseInstallation;
import com.parse.ParsePush;
import com.parse.SaveCallback;
import static chipset.lugmnotifier.resources.Constants.APPLICATION_ID;
import static chipset.lugmnotifier.resources.Constants.CLIENT_KEY;
public class ParseInitApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
Parse.initialize(this, APPLICATION_ID, CLIENT_KEY);
ParsePush.subscribeInBackground("", new SaveCallback() {
#Override
public void done(ParseException e) {
if (e != null) {
Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");
} else {
Log.e("com.parse.push", "failed to subscribe for push", e);
}
}
});
ParseInstallation.getCurrentInstallation().saveInBackground();
}
}
HomeActivity.java
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import com.parse.FindCallback;
import com.parse.ParseAnalytics;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import org.duncavage.swipetorefresh.widget.SwipeRefreshLayout;
import java.util.List;
import chipset.lugmnotifier.resources.Functions;
import chipset.lugmnotifier.resources.NotificationListViewAdapter;
import de.keyboardsurfer.android.widget.crouton.Crouton;
import de.keyboardsurfer.android.widget.crouton.Style;
import static chipset.lugmnotifier.resources.Constants.KEY_CLASS_NOTIFICATION;
import static chipset.lugmnotifier.resources.Constants.KEY_DETAIL;
import static chipset.lugmnotifier.resources.Constants.KEY_TITLE;
import static chipset.lugmnotifier.resources.Constants.PASSWORD;
public class HomeActivity extends Activity {
ListView notificationsListView;
SwipeRefreshLayout notificationSwipeRefreshLayout;
Functions functions = new Functions();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ParseAnalytics.trackAppOpened(getIntent());
notificationSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.notificationSwipeRefreshLayout);
notificationsListView = (ListView) findViewById(R.id.notificationListView);
notificationSwipeRefreshLayout.setColorScheme(R.color.alizarin, R.color.emerald, R.color.peterRiver, R.color.sunFlower);
notificationSwipeRefreshLayout.setActionBarSwipeIndicatorText(R.string.swipe_to_refresh);
notificationSwipeRefreshLayout.setActionBarSwipeIndicatorRefreshingText(R.string.loading);
notificationSwipeRefreshLayout.setActionBarSwipeIndicatorBackgroundColor(
getResources().getColor(R.color.alizarin));
notificationSwipeRefreshLayout.setActionBarSwipeIndicatorTextColor(
getResources().getColor(R.color.clouds));
notificationSwipeRefreshLayout.setActionBarSwipeIndicatorRefreshingTextColor(
getResources().getColor(R.color.clouds));
notificationSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
new FetchData().getNotifications();
}
});
new FetchData().getNotifications();
}
private class FetchData {
public void getNotifications() {
if (functions.isConnected(getApplicationContext())) {
notificationSwipeRefreshLayout.setRefreshing(true);
ParseQuery<ParseObject> query = ParseQuery.getQuery(KEY_CLASS_NOTIFICATION);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> parseObjects, ParseException e) {
notificationSwipeRefreshLayout.setRefreshing(false);
final String[] title = new String[parseObjects.size()];
final String[] detail = new String[parseObjects.size()];
if (e == null) {
for (int i = 0; i < parseObjects.size(); i++) {
title[i] = parseObjects.get(i).getString(KEY_TITLE);
detail[i] = parseObjects.get(i).getString(KEY_DETAIL);
}
notificationsListView.setAdapter(new NotificationListViewAdapter(getApplicationContext(), title, detail));
notificationsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Crouton.showText(HomeActivity.this, title[i] + " - " + detail[i], Style.INFO);
}
});
} else {
Crouton.showText(HomeActivity.this, e.getMessage(), Style.ALERT);
}
}
});
} else {
notificationSwipeRefreshLayout.setRefreshing(false);
Crouton.showText(HomeActivity.this, "No internet connection", Style.ALERT);
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_admin) {
final EditText passwordEditText = new EditText(HomeActivity.this);
passwordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
builder.setTitle("Admin Panel");
builder.setMessage("Enter the admin password");
builder.setView(passwordEditText);
builder.setPositiveButton("LOGIN", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if (passwordEditText.getText().toString().equals(PASSWORD)) {
startActivity(new Intent(HomeActivity.this, AdminActivity.class));
} else {
Crouton.showText(HomeActivity.this, "Incorrect Password", Style.ALERT);
}
}
});
builder.setNeutralButton("CANCEL", null);
builder.create();
builder.show();
}
return super.onOptionsItemSelected(item);
}
}
StackTrace (of the crash)
10:46:02.302 15739-15739/chipset.lugmnotifier E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: chipset.lugmnotifier, PID: 15739
java.lang.RuntimeException: Unable to start receiver com.parse.ParsePushBroadcastReceiver: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat= flg=0x1000c000 (has extras) }
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2414)
at android.app.ActivityThread.access$1700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat= flg=0x1000c000 (has extras) }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)
at android.app.Instrumentation.execStartActivitiesAsUser(Instrumentation.java:1481)
at android.app.ContextImpl.startActivitiesAsUser(ContextImpl.java:1080)
at android.content.ContextWrapper.startActivitiesAsUser(ContextWrapper.java:344)
at android.content.ContextWrapper.startActivitiesAsUser(ContextWrapper.java:344)
at android.app.TaskStackBuilder.startActivities(TaskStackBuilder.java:221)
at android.app.TaskStackBuilder.startActivities(TaskStackBuilder.java:232)
at android.app.TaskStackBuilder.startActivities(TaskStackBuilder.java:208)
at com.parse.TaskStackBuilderHelper.startActivities(TaskStackBuilderHelper.java:19)
at com.parse.ParsePushBroadcastReceiver.onPushOpen(ParsePushBroadcastReceiver.java:202)
at com.parse.ParsePushBroadcastReceiver.onReceive(ParsePushBroadcastReceiver.java:108)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2407)
            at android.app.ActivityThread.access$1700(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
I want to open HomeActivity on notification tap. Any help will be appreciated. If any other file is required, please let me know I'll add it.
Quoting from below post by Ahmad Raza
Exception when opening Parse push notification
You can extend ParsePushBroadcastReceiver and override onPushOpen method.
public class Receiver extends ParsePushBroadcastReceiver {
#Override
public void onPushOpen(Context context, Intent intent) {
Log.e("Push", "Clicked");
Intent i = new Intent(context, HomeActivity.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Use it in manifest, (Instead of using ParsePushBroadcastReceiver)
<receiver
android:name="your.package.name.Receiver"
android:exported="false" >
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
After a long effort, it worked for me:
ParsePushApplication.java
public class ParsePushApplication extends Application {
#Override
public void onCreate(){
super.onCreate();
Parse.initialize(this, "App_Key", "Client_Key");
ParseInstallation.getCurrentInstallation().saveInBackground();
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ParseAnalytics.trackAppOpenedInBackground(getIntent());
try {
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
String jsonData = extras.getString("com.parse.Data");
JSONObject json;
json = new JSONObject(jsonData);
String pushStore = json.getString("alert");
Toast.makeText(MainActivity.this, pushStore, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Receiver.java
public class Receiver extends ParsePushBroadcastReceiver {
private static final String TAG = "MyNotificationsReceiver";
#Override
public void onPushOpen(Context context, Intent intent) {
Log.e("Push", "Clicked");
Intent i = new Intent(context, MainActivity.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package.name">
<!-- IMPORTANT: Change "your.package.name" to match your app's package name. -->
<application
android:name=".ParsePushApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<meta-data
android:name="com.parse.push.notification_icon"
android:resource="#drawable/push_icon"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver
android:name="your.package.name.Receiver"
android:exported="false" >
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!--
IMPORTANT: Change "your.package.name" to match your app's package name.
-->
<category android:name="your.package.name" />
</intent-filter>
</receiver>
</application>
<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.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!--
IMPORTANT: Change "your.package.name.permission.C2D_MESSAGE" in the lines below
to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission android:protectionLevel="signature"
android:name="your.package.name.permission.C2D_MESSAGE" />
<uses-permission android:name="com.zeeroapps.parsetutorial_cli.permission.C2D_MESSAGE" />
</manifest>

Categories