Create PendingIntent for notification without specifying activity - java

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

Related

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

scheduling alarmmanager not working on reboot my phone

It's toasting until I didn't restart my phone but after restarting broadcastreceiver2 doesn't receive and nothing happens.
I followed http://stacktips.com/tutorials/android/how-to-start-an-application-at-device-bootup-in-android and many other but nothing happens.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.k2.alarmmanagerdemo">
<uses-permission android:name="android.permission.VIBRATE" />
<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>
<receiver android:name="com.k2.alarmmanagerdemo.MyBroadcastReceiver"
android:enabled="true">
</receiver>
<receiver android:name="com.k2.alarmmanagerdemo.BroadCastRecevier2"
android:enabled="true"
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.QUICKBOOT_POWERON"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
MainActivity.java
public class MainActivity extends Activity {
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startAlert();
}
});
}
public void startAlert() {
EditText text = (EditText) findViewById(R.id.time);
int i = Integer.parseInt(text.getText().toString());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),
1000 * 5,pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " seconds",Toast.LENGTH_LONG).show();
}
}
MyBroadcastReceiver.java
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm...." + System.currentTimeMillis(), Toast.LENGTH_SHORT).show();
Log.i("Alarm.", "alarm called" + System.currentTimeMillis());
}
}
BroadCastRecevier2.java
public class BroadCastRecevier2 extends BroadcastReceiver {
MainActivity activity = new MainActivity();
#Override
public void onReceive(Context context, Intent intent) {
activity.startAlert();
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Intent i = new Intent();
i.setClassName("com.k2.alarmmanagerdemo",
"com.k2.alarmmanagerdemo.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Toast.makeText(context, "BOOT", Toast.LENGTH_SHORT).show();
Log.i("myboot","boot compleated inside");
}
}
}
Remove this line from your <receiver> declaration for BroadcastReceiver2:
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
That line in the <receiver> tag is telling the system that only packages which have this permission are allowed to send your class the Intent. Its' likely confusing the system and preventing the Intent from being sent to your receiver.
You may also find this article helpful when learning about alarms and the boot complete receiver.

Alarm not working on some android version or device like oppo when app close mostly on lollipop

my alarm manager not working on lollipop version when app close while it's working on other Android version marshmallow, Nougat, perfectly
alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReciver.class);
intent.putExtra("msg", msgstring);
intent.putExtra("num", recipstring);
intent.putExtra("recipname", name);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, number, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);``
here is my alarm receiver:
public class AlarmReciver extends WakefulBroadcastReceiver {
#Override public void onReceive(Context context, Intent intent) {
Notification.Builder builder= new Notification.Builder(context);
builder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis()).setColor(Color.WHITE)
.setSmallIcon(R.mipmap.ic_launcher).setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND)
.setContentTitle("test")
.setContentText(message);
NotificationManager notificationManager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
dbhelper.insertdilvery(message, name);
}
}
Manifest file:
<receiver android:name="com.simulatorgames3d.EventsNotifications.alarmreceiver.AlarmReciver"
android:process=":remote"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
permissions
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
herr is logcat pic when app not close
locat when app close and alrm not fire

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.

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.

Categories

Resources