Cancel different onGoing notifications - java

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.

Related

Create PendingIntent for notification without specifying activity

Is there any way to create PendingIntent for notification or to show notification from "data" layer.
In that layer I don't have activity class.
So can Intent automatically choose one activity that is marked like "default" or "launcher"?
<activity
android:name=".feature.splash.SplashActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You can use PendingIntent.getBroadcast() :
Intent broadcastIntent = new Intent(mContext, NotificationReceiver.class);
broadcastIntent.putExtra(Const.NOTIFICATION_ID,"1";
PendingIntent cancleIntent = PendingIntent.getBroadcast(mContext,
0, broadcastIntent, PendingIntent.FLAG_CANCEL_CURRENT);
public class NotificationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.hasExtra(Const.NOTIFICATION_ID)) {
int notificationId = intent.getIntExtra(Const.NOTIFICATION_ID, 0);
// do your logic like cancle notification
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(notificationId);
}
}
}

Alarm Manager not woking

I am using AlarmManager on clicking floatingActionButton but it is not calling the alarm class. I tested the floatingActionButton via Toasts but it is working fine but Toast and vibrator in My_Alarm Class is not working even tough i have enebled permission in Manifests. Kindly point out the problem!
Below code main activity (Alarm Manager in floatingActionButton)
floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AlarmManager.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,5,intent,0);
sendBroadcast(intent);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000,pendingIntent);
}
});
}
This is my Alarm class (Toast and Vibrate not working)
public class My_Alarms extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Fuck This Shit", Toast.LENGTH_LONG).show();
Vibrator v = (Vibrator)context.getSystemService(context.VIBRATOR_SERVICE);
v.vibrate(10000);
}
}
And this is my Manifest file (I have enabled permission)
<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=".Activity_Time_Setting"
android:grantUriPermissions="true"
></activity>
<activity android:name=".MainActivity">
android:grantUriPermissions="true"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
android:grantUriPermissions="true"
</intent-filter>
</activity>
<receiver android:name=".My_Alarms"/>
</application>
Replace this line of code :
Intent intent = new Intent(MainActivity.this, AlarmManager.class);
with this :
Intent intent = new Intent(MainActivity.this, My_Alarms.class);
Also you don't need to use
sendBroadcast(intent);
this line because you are already passing intent in pending intent
At last try to to remove toast from your broadcast reciver

TaskStackBuilder not returning to MainActivity

So i was working out on Android notifications,
I tried out one the examples of notification on android documentation:
In one of the examples it shows, how to open an activity from notification.
So the Problem is that: When i try to press back button on the activity opened by notification it should go back to MainActivity but the app closes out.
ResultActivity is just the default empty Activity with a TextView
MainActivity.java:
// The id of the channel.
String CHANNEL_ID = "my_channel_01";
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(android.R.drawable.stat_notify_more)
.setContentTitle("Event tracker")
.setContentText("Events received");
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
String[] events = new String[6];
// / Sets a title for the Inbox in expanded layout
inboxStyle.setBigContentTitle("Event tracker details:");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Moves events into the expanded layout
for (int i=0; i < events.length; i++) {
events[i] = "Event " + i;
inboxStyle.addLine(events[i]);
}
// Moves the expanded layout object into the notification object.
mBuilder.setStyle(inboxStyle);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="justjava.andriod.example.com.notificationchannel">
<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>
<activity android:name=".ResultActivity"
android:parentActivityName=".MainActivity"
/>
</application>
</manifest>
Replace the line:
stackBuilder.addParentStack(MainActivity.class);
With:
stackBuilder.addParentStack(ResultActivity.class);
Also add the <meta-data tag to your activity in the manifest if you're testing on API level less than 16
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>

Android alarm manager doesn't trigger my event

I'm having some trouble adding notifications to my app. I would like a notification everyday at 10am, for example. Here's my code:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private PendingIntent alarmIntent;
private AlarmManager alarmMgr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setAlarm();
}
private void setAlarm() {
alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 00);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 3, alarmIntent);
}
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.turnu_a.todolist">
<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>
<activity android:name=".taskView.newTaskActivity" />
<activity android:name=".taskView.showTaskDetailsActivity" />
<activity android:name=".taskView.updateTaskActivity" />
<activity android:name=".settings.settingsActivity"></activity>
<receiver android:name=".Alarm.AlarmReceiver"></receiver>
</application>
The thing is, even though I change the time for the trigger to happen in a minute or two (in order to test my code), absolutely nothing happens. I've tried to create a notification in the "setAlarm()" method and it worked, meaning the problem doesn't come from the creation of the notification but the alarmManager not being triggered.
I'd appreciate any help. Thanks.
So... I removed this line:
calendar.setTimeInMillis(System.currentTimeMillis());
from the "setAlarm()" method and now it works.
I am glad it works but a bit surprised the problem came from here (though in hindsight, I don't think this line is quite useful anyway).
Anyway, problem solved, thanks to everyone who tried to help.

Push notification not working - Android

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);

Categories

Resources