Issue
I have been trying to open my application to my MainActivity, and load a fragment, from a notification and I cannot seem to do so successfully. My most successful build appears to do everything successfully except actually open the app. The app will be completely closed and I'll receive the notification and tap on it. The phone drop down menu will retract and act like it's about to open the app, then doesn't. When I open the app manually the application has been create and navigated successfully to the fragment. It just didn't open my app.
I'll list relevant code below:
Manifest Code
<?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.easy.planner">
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/Theme.Planner"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleInstance" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>=
<receiver
android:name=".ReminderReceiver"
android:exported="true"
android:theme="@android:style/Theme.NoDisplay"/>
</application>
</manifest>
Code in ReminderReceiver
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP & Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("id", reminder.getId());
final PendingIntent pendingIntent;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){
pendingIntent = PendingIntent.getActivity(context, reminder.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);}
else{
pendingIntent = PendingIntent.getActivity(context, reminder.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);}
NotificationHelper helper = new NotificationHelper(context);
String title = "title";
String message = "message";
NotificationCompat.Builder builder = helper
.getReminderNotification(context, title, message,pendingIntent, true);
helper.GetManager().notify(reminder.getId(), builder.build());
Notification Helper
public class NotificationHelper extends ContextWrapper {
public static final String TAG = "Notification Helper";
public static final String REMINDERS_CHANNEL_ID = "id";
public static final String REMINDERS_CHANNEL_NAME = "name";
private NotificationManager manager;
public NotificationHelper(Context base) {
super(base);
CreateChannels(base);
}
public static boolean CreateChannels(Context context)
{
NotificationChannel reminders = new NotificationChannel(REMINDERS_CHANNEL_ID, REMINDERS_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
reminders.enableLights(true);
reminders.enableVibration(true);
NotificationManager manager = GetManager(context);
manager.createNotificationChannel(reminders);
return true;
}
public NotificationCompat.Builder getReminderNotification(Context context, String title, String message, PendingIntent pendingIntent, boolean autoCancel)
{
return new NotificationCompat.Builder(getApplicationContext(), REMINDERS_CHANNEL_ID)
.setSmallIcon(R.drawable.notification_temp)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(pendingIntent)
.setAutoCancel(autoCancel);
}
I've been looking at plenty of other stackoverflow pages and have yet to find a solution.
Solution
you can update your ReminderReceiver's code to this and check if it is working
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP &
Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("id", reminder.getId());
final PendingIntent pendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(context, reminder.getId(), notificationIntent /*update*/ , PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(context, reminder.getId(), notificationIntent /*update*/ , PendingIntent.FLAG_UPDATE_CURRENT);
}
NotificationHelper helper = new NotificationHelper(context);
String title = "title";
String message = "message";
NotificationCompat.Builder builder = helper
.getReminderNotification(context, title, message, pendingIntent, true);
helper.GetManager().notify(reminder.getId(), builder.build());
I think you are apssing wrong intent into your NotificationBuilder.
Hope this works!!
Answered By - Ajay Makwana
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.