Push notification not working - Android - java

I am trying to send a push notification on a button click event. But don't know why, but it is not working. It doesn't show any error though. Can anybody find any error in the code?
public class Verify extends AppCompatActivity {
public static String TAG=Verify.class.getSimpleName();
NotificationCompat.Builder notification;
private static final int uniqueID=12345;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verify);
notification=new NotificationCompat.Builder(this);
notification.setAutoCancel(true);
}
public void onVerify(View v)
{
this.defineNotification();
//other code follows
}
public void defineNotification()
{
notification.setContentTitle("Successfully Signed Up");
notification.setContentText("Hi, you just Signed Up as a Vendor");
notification.setWhen(System.currentTimeMillis());
Intent intent=new Intent(this,OtherActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
NotificationManager nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(uniqueID,notification.build());
//Log.i(TAG, "coming here in notification");
}
Here is the Android Manifest Code :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sachinparashar.xyz">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SignUp"
android:label="SignUp"
android:theme="#style/AppTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Verify"
android:label="#string/title_activity_verify"
android:theme="#style/AppTheme"/>
<activity
android:name=".VendorDetails"
android:label="Vendor Details"
android:theme="#style/AppTheme"/>
<activity
android:name=".OrderTypes"
android:label="Orders"
android:theme="#style/AppTheme" />
</application>

change this :
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
with this :
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

Insted of
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
use
PendingIntent pendingIntent=PendingIntent.getActivity(Verify.this,0,intent,0);

Related

Alarm Manager does not work in background

I used alarm manager in my program,but for the first time I run it, It just worked when my program was open and when it was close it did not work in the background, but in the second time it worked in the background too.
MainActivity :
public class MainActivity extends AppCompatActivity {
AlarmManager alarmManager;
Intent intent;
PendingIntent pendingIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
intent = new Intent(MainActivity.this, AlertReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+60000, pendingIntent);
}
}
AlertReceiver :
public class AlertReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("Log", "onReceiver!!");
}
}
AndroidManifest :
<?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.yadame">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<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" android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlertReceiver"/>
</application>

Alarm manager execute method

This is part of a code that I combine together from google and youtube, to execute "turnOnLed" when the alarm go off. For some reason it doesnt execute and I don't know how to make it work. Can someone help? I've Google it, but no succes.
Thank you.
private void startAlarm(Calendar c)
{
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if (c.before(Calendar.getInstance()));
{
c.add(Calendar.DATE, 1);
}
Intent intent = new Intent(this, Control.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(), pendingIntent);
}
public class Control extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
turnOnLed(); //method to turn on
}
}
And the Manifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<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="art.control4.IntroScreen"
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="art.control4.DeviceList"
android:label="#string/app_name">
</activity>
<activity
android:name="art.control4.ledControl"
android:label="#string/app_name">
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
</activity>
<receiver android:name=".ledControl$Control"
android:process=":remote"/>

android broadcast receiver and service to get notification

hi i have used the following code in receiver class
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("the time is right","yay!");
Intent i = new Intent(context, AlarmServie.class);
context.startService(i);
}
}
here i the code which i used in service class
public class AlarmServie extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.e("onStart:","came" );
/* NotificationManager notifyman = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent main_activity = new Intent(this.getApplicationContext(), MainActivity.class);
PendingIntent o = PendingIntent.getActivity(this, 0, main_activity, 0);
/*Notification noti = new Notification.Builder(this)
.setContentTitle("Reminder to Pill")
.setContentText("Click for info")
.setAutoCancel(true)
.setContentIntent(o)
.build();*/
NotificationManager nm = (NotificationManager) this.getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
Intent in = new Intent(this.getApplicationContext(), MainActivity.class);
PendingIntent pending = PendingIntent.getactivity(this, 0, in, 0);
NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(AlarmServie.this);
mBuilder.setContentTitle("Pill Reminder");
mBuilder.setContentText("CLick here to View");
//mBuilder.setSound(sound);
TaskStackBuilder ts=TaskStackBuilder.create(this);
ts.addParentStack(MainActivity.class);
nm.notify(9999,mBuilder.build());
}}
i created this code to get notification.when i run the app the receiver class is triggered but it is not moving to service class to invoke notification.can anyone say whats wrong in the code or say me how to get notification using broadcast receiver and service in a detailed tutorial
here is the manifest file and say me where to put the code exactly
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmReceiver"></receiver>
<activity android:name=".Set"></activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
please explain in detail what the problem in code.please tell exactly where to put the service tag
You must declare service in manifest also.
Intent to start service is sent to system then system tries to find right application component to handle this particular intent using information passed in manifest files.
It should be enough, but I'm not sure if you put service in the default package.
<application>
....
<service android:name=".AlarmServie"/>
....
</application>
Your Manifest should look something like this
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//here is your service
<service android:name=".AlarmServie" android:enabled="true"/>
<receiver android:name=".AlarmReceiver"></receiver>
<activity android:name=".Set"></activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>

Cancel different onGoing notifications

I have different Ongoing notifications. When an addAction() button is pressed a BroadcastReceiver is called and my notification is cleaned. The problem is the addAction() always pass the id of the last notification added, not the one that you pressed:
Here is the method adding the notification:
public void pushNotification(View v){
String text = editText.getText().toString().trim();
editText.setText("");
int millis = (int) (System.currentTimeMillis() % Integer.MAX_VALUE);
Intent notificationIntent = new Intent(mContext, NotificationReceiver.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("id", millis);
PendingIntent pIntent = PendingIntent.getBroadcast(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(mContext);
notification.setContentTitle("");
notification.setContentText(text);
notification.setSmallIcon(R.mipmap.ic_launcher);
notification.setOngoing(true);
notification.addAction(R.mipmap.ic_launcher, "Dismiss", pIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(millis, notification.build());
}
Receiver:
public class NotificationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent){
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(intent.getIntExtra("id", 0));
}
}
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="PACKAGE_NAME">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".NotificationReceiver" />
</application>
I found the solution, just add FLAG_ONE_SHOT to your pending intent and now all the extras will be different.

getting action of buttons on receive() method which is a method of broadcast receiver but its show a null

I am getting action of buttons by getaction on receive() method which is a method of broadcast receiver but its sho a null plz tell me what I do in this to get a value which is not null
+my notify method is following
private void Notify(String notificationTitle, String notificationMessage)
{
String ns=Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager=(NotificationManager)getSystemService(ns);
#SuppressWarnings("deprecation")
Notification notification=new Notification(R.drawable.bg,"Time",System.currentTimeMillis());
RemoteViews notificationView=new RemoteViews(getPackageName(),R.layout.main);
Intent notificationIntent=new Intent(this,PlayerAudioActivity.class);
PendingIntent pendingNotificationIntent=PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentView=notificationView;
notification.flags|=Notification.FLAG_NO_CLEAR;
//supposed button call intent
Intent switchIntent=new Intent(this,MyReceiver.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(this, 0, switchIntent, 0);
notificationView.setOnClickPendingIntent(R.id.btnPrevious, pendingIntent);
notificationManager.notify(1, notification);
}
-my broadcastreceiver class is following
public class MyReceiver
extends BroadcastReceiver {
private static final String TAG = "waaaawoooooooooOOOOOOjnjkhdfku";
public void onReceive(Context context, Intent intent) {
final String action=intent.getAction();
// if(AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)){
Log.d(TAG, ""+action);
// Bundle extras=intent.getExtras();
// String ieString=extras.getString("Locale");
// try{
// final int Appwidgid=extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
// if(Appwidgid!=AppWidgetManager.INVALID_APPWIDGET_ID)
// {
// this.onDeleted(context, new int[] { Appwidgid });
// }
// else {
// onReceive(context, intent);
// }
// context.startService(new Intent(context,PlayerAudioActivity.class));
// Log.i(TAG,"Starting Service ConnectivityListener");
// }catch(Exception e){
// Log.e(TAG,e.toString());
// }
// }
}
private void onDeleted(Context context, int[] is) {
// TODO Auto-generated method stub
}
}
+manifest file is following
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Audio.audioplayer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".MyReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/><action
android:name="android.net.conn.CONNECTIVITY_CHANGE"/><action
android:name="android.net.conn.DATA_ACTIVITY_CHANGE"/>
</intent-filter>
</receiver>
<activity
android:name="com.Audio.audioplayer.PlayerAudioActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<data android:scheme="tel" />
</intent-filter>
</activity>
<activity
android:name=".PlayListActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.Audio.audioplayer.PlayerAudioActivity" />
</activity>
</application>
</manifest>
As adamp mentioned in this post you cannot access an action on an intent that you've not set:
{...}
//supposed button call intent
Intent switchIntent=new Intent(this, MyReceiver.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(this, 0, switchIntent, 0);
{...}
You have to add your designated action to that intent by using setAction().
Something like this:
{...}
Intent switchIntent=new Intent(this, MyReceiver.class);
switchIntent.setAction("YOUR_ACTION");
PendingIntent pendingIntent=PendingIntent.getBroadcast(this, 0, switchIntent, 0);
{...}
ps: just pseudo code

Categories

Resources