I have parse notifications set up for my android app using Parse 1.7.1 sdk version.
But in the new android this method with parse says to call is depreciated.
PushService.setDefaultPushCallback(this, MainActivity.class);
But when it is removed the notification is sent from parse.com as I can see on the website but it does not arrive to the phone?
How can this be changed so that the push will arrive? Without using the depreciated method?
Thanks for the help in advance.
Try extending ParsePushBroadcastReceiver class and and use its
OnPushRecieve (to do something before notification is shown in status bar)
OnPushOpen (to do some action when user open's push for example open an activity)
getNotification and
onPushDismiss methods
And in manifest file replace
<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>
with this :
<receiver
android:name="com.example.parse.Notifications.NotificationsReciever"
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>
And if you want to open an activity onPushOpen, here is a sample:
#Override
protected void onPushOpen(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent i = new Intent(context, PushNotificationHandler.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
Here is a sample class that extends ParsePushBroadcastReciever class
public class NotificationsReciever extends ParsePushBroadcastReceiver {
#Override
protected Class<? extends Activity> getActivity(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
return ParseStarterProjectActivity.class;
}
#Override
protected Notification getNotification(Context context, Intent intent) {
// TODO Auto-generated method stub
return super.getNotification(context, intent);
}
#Override
protected void onPushDismiss(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onPushDismiss(context, intent);
}
#Override
protected void onPushOpen(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent i = new Intent(context, PushNotificationHandler.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
#Override
protected void onPushReceive(Context context, Intent intent) {
//here You can handle push before appearing into status e.g if you want to stop it.
super.onPushReceive(context, intent);
}
}
Related
I am trying to create a simple service in java that will run in the background. I want to play a song on the phone even though the app is closed. I read some articles and watched tutorials but my service still stops when I close the app.
1) I changed my manifest
<service
android:name="com.example.myapplication.ServiceSound"
android:exported="true"
android:enabled="true">
</service>
2) I returned START_STICKY
This is manifest
<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
android:name="com.example.myapplication.ServiceSound"
android:exported="true"
android:enabled="true">
</service>
</application>
This is the service
public class ServiceSound extends Service {
MediaPlayer player;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
player = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
player.start();
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
player.stop();
}
}
And this is how I start the service in MainActivity.java
startService(new Intent(getBaseContext(),ServiceSound.class));
I think you need Broadcast Receiver.
So now
1- Create broadcast receiver class and call your Service class.
public class MyBroadcaseReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context,ServiceSound.class));
Log.e(TAG, "onReceive: BroadCast is started");
}
}
2- Your Service class define this
public static String str_receiver = "Your_Package_Name.receiver";
on startCommand start your broadcast class
public int onStartCommand(Intent intent, int flags, int startId) {
player = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
player.start();
return START_STICKY;
Intent intent1 = new Intent(this,MyBroadcaseReceiver.class);
sendBroadcast(intent1);
return START_STICKY;
}
Now in your MainActivity class create an object for broadcast
private BroadcastReceiver broadcastReceiver;
inside onCreate() define the object
broadcastReceiver = new MyBroadcaseReceiver();
Finaly call onResume() and onPause() inside the MainActivity.
#Override
protected void onResume() {
super.onResume();
registerReceiver(broadcastReceiver, new IntentFilter(MyService.str_receiver));
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
}
Don't forget to call broadcast reciever in your Manifest file
<receiver android:name=".MyBroadcaseReceiver"></receiver>
This is my activity class:
public class activity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
Intent i = new Intent(activity.this, Service.class);
PendingIntent pIntent = PendingIntent.getService(getApplicationContext(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar c=Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY,mHour);
c.set(Calendar.MINUTE,mMinute);
c.set(year,month,date);
c.setTimeInMillis(System.currentTimeMillis());
aManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pIntent);
Toast.makeText(getApplicationContext(), "Service triggered", Toast.LENGTH_SHORT).show();
startService(i);
}
}
And this is my service class:
public class Smscreator extends Service {
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent i, int flags, int startId) {
// TODO Auto-generated method stub
Context context=getApplicationContext();
Toast.makeText(context, "This is the service.",
Toast.LENGTH_LONG).show();
return START_STICKY;
}
}
This is my Manifest class:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
<service
android:name=".Service"
android:enabled="true"
android:exported="true"></service>
<activity android:name=".activity"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The variables mHour,mMinute,year,month and date are certain values for the date and time that depends on the user.
Now when I execute this code, the service gets triggered instantly; it completely ignores the date and time specified by the user. Can someone please help me with this?
Edit: I am currently working on Android M.
I would like my app to detect if a new outgoing call happened
I want to do something like this :
if (there was a new outgoing call) {
do...
} else {
do...
}
You can use the android Broadcast Reciever. To use it all you have to do is first,
Create a new handler class to handle the broadcast receiver.
public class OutgoingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//You can do this to get the phone number, it is only optional
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
//Handle outgoing call event here
}
}
Then register it in the android manifest so it knows where this method is.
<receiver android:name=".OutgoingCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
Finally, create a permission in the android manifest.
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
And that should do it, hope I could help. :)
Create a broadcast receiver to catch the outgoing calls:
public class CallReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
// your code
}
}
Then register it in the manifest:
<receiver android:name=".CallReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Lastly set the permissions:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
Detect outgoing phone call event
1)Create OutgoingCallBroadcastReceiver
public class OutgoingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(OutgoingCallReceiver.class.getSimpleName(), intent.toString());
Toast.makeText(context, "Outgoing call catched!", Toast.LENGTH_LONG).show();
//TODO: Handle outgoing call event here
}
}
Register OutgoingCallBroadcastReceiver in AndroidManifest.xml
Add permission in AndroidManifest.xml
Hello i create one broadcast test but no work
Manifest:
<receiver android:name=".BeaconsBroadcast"
android:exported="false">
<intent-filter>
<action android:name="com.example.android.kontacktestbeacons.BeaconsBroadcast"/>
</intent-filter>
</receiver>
in my MainActivity:
protected void onStop() {
super.onStop();
try{
Log.e("ENTRO ","ENTRO");
Intent i = new Intent();
i.setAction("com.example.android.kontacktestbeacons.BeaconsBroadcast");
startService(i);
}catch (Exception e){Log.e("ERROR","ERRR");}
}
mi broadcast class:
public class BeaconsBroadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("ENTRO ","EBTROPOOOOO RECIVE");
Toast.makeText(context, "Se ha pulsado el botón.", Toast.LENGTH_SHORT)
.show();
}
}
in logcat:
12-02 11:12:59.551 28588-28588/com.mydomain.myapplication W/ContextImpl﹕ Implicit intents with startService are not safe: Intent { act=com.example.android.kontacktestbeacons.BeaconsBroadcast } android.content.ContextWrapper.startService:494 com.example.android.kontacktestbeacons.MainActivity.onStop:101 android.app.Instrumentation.callActivityOnStop:1235
12-02 11:12:59.552 927-2008/? W/ActivityManager﹕ Unable to start service Intent { act=com.example.android.kontacktestbeacons.BeaconsBroadcast } U=0: not found
where my error?
You are declaring BeaconsBroadcast as a BroadcastReceiver but are using startService(i) to invoke it which is causing the issue.
You need to use the sendBroadcast(i) function to send a broadcast to a BroadcastReceiver.
Also Since you have set android:exported="false" for BroadcastReceiver use
LocalBroadcastManager.getInstance(Context context).sendBroadcast(Intent)
Hello im fine by manifest
<receiver android:name="BeaconsBroadCast">
<intent-filter>
<action android:name="com.tutorialspoint.CUSTOM_INTENT">
</action>
</intent-filter>
</receiver>
in application tag
I would like to start a service when the phone boot but does not work and I do not understand why. In my Package i created a class
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootPhone extends BroadcastReceiver{
Intent intent;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
intent = new Intent(context, Service.class);
context.startService(intent);
}
}
My Service.class is
public class Service extends Service{
Notify notify;
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
notify = new Notify(this);
}
}
In my Manifest i put
<service android:name="Servicer"></service>
<receiver android:name="BootPhone"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
But, why it does'n work? Where is the problem/error?
you have to declare user permission in manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />