My manifest file is like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.navigationdrawer" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
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=".DetailActivity"
android:label="#string/title_activity_detail"
android:parentActivityName=".MainActivity" >
<!-- The meta-data element is needed for versions lower than 4.1 -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
>
</activity>
</application>
</manifest>
and the code I am using to start the DetailActivity from MainActivity is
Intent resultIntent = new Intent(this, DetailActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(DetailActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, builder.build());
Related
I'm using FCM to send push notifications to my app; the expected behaviour when the user taps the notification is to launch the app as normal, like tapping the launcher app icon.
At the moment, when user tap on the notification, spend more than 30 seconds before app is opened. Notification go away, but the app does not opens... Not even a white screen, nothing.
When onMessageReceived is triggered, displayNotification method is called.
private void displayNotification(String title, String body) {
createNotificationChannel();
PendingIntent contentIntent = generateNotificationContentIntent();
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CONTENTS_CHANNEL_ID)
.setSmallIcon(R.drawable.logo_no_bg_white)
.setContentTitle(title)
.setContentText(body)
.setContentIntent(contentIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(new Random().nextInt(), notificationBuilder.build());
}
private PendingIntent generateNotificationContentIntent(){
Intent intent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(intent);
return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
return;
}
NotificationChannel notificationChannel = new NotificationChannel(CONTENTS_CHANNEL_ID, CONTENTS_CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription(CONTENTS_CHANNEL_ID);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(notificationChannel);
}
My onCreate MainActivity method simply checks wether the user is logged, and if so, launches the HomeActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PACKAGE_NAME = getApplicationContext().getPackageName();
//Read shared preferences.
sharedPref = getSharedPreferences(PACKAGE_NAME, Context.MODE_PRIVATE);
if(sharedPref.contains("token") || sharedPref.getBoolean("isGuest", false)){
//Redirect to home
Intent homeIntent = new Intent(getApplicationContext(), HomeActivity.class );
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(homeIntent);
finish();
}
loadTowns(this);
}
What am I doing wrong?
UPDATE: Manifest
<?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="my.awesome.package">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<application
android:allowBackup="false"
android:icon="#mipmap/icon"
android:label="#string/app_name"
android:roundIcon="#mipmap/icon"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.com.vansuita.pickimage.provider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/picker_provider_paths" />
</provider>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/logo_no_bg_white" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#android:color/holo_orange_dark" />
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:theme="#style/NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:screenOrientation="portrait"
android:theme="#style/NoActionBar" />
<activity
android:name=".HomeActivity"
android:screenOrientation="portrait"
android:theme="#style/WhiteActionBarTheme" />
<activity
android:name=".ChannelProfileActivity"
android:label="#string/channel"
android:screenOrientation="portrait"
android:theme="#style/WhiteActionBarTheme" />
<activity
android:name=".TownProfileActivity"
android:label="#string/town"
android:screenOrientation="portrait"
android:theme="#style/WhiteActionBarTheme" />
<activity
android:name=".ContentActivity"
android:label="#string/content"
android:screenOrientation="portrait"
android:theme="#style/WhiteActionBarTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".HomeActivity" />
</activity>
<activity
android:name=".ContentsListActivity"
android:label="#string/contents"
android:screenOrientation="portrait"
android:theme="#style/WhiteActionBarTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".HomeActivity" />
</activity>
<activity
android:name=".QuestionResultsActivity"
android:label="#string/question"
android:screenOrientation="portrait"
android:theme="#style/WhiteActionBarTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".HomeActivity" />
</activity>
<activity
android:name=".ForgetPasswordActivity"
android:label="#string/DidYouForgetPassword"
android:screenOrientation="portrait"
android:theme="#style/WhiteActionBarTheme" />
<activity
android:name=".ValidationProcesActivity"
android:label="#string/verify_user"
android:screenOrientation="portrait"
android:theme="#style/WhiteActionBarTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".HomeActivity" />
</activity>
<activity
android:name=".QuestionVoteActivity"
android:label="#string/question"
android:screenOrientation="portrait"
android:theme="#style/WhiteActionBarTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".HomeActivity" />
</activity>
<activity
android:name=".NewUserActivity"
android:label="#string/new_user"
android:screenOrientation="portrait"
android:theme="#style/WhiteActionBarTheme" />
<activity
android:name=".NewUserSuccessActivity"
android:label="#string/new_user"
android:screenOrientation="portrait"
android:theme="#style/WhiteActionBarTheme" />
<service android:name=".firebase.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".firebase.MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<activity
android:name=".NotificationsActivity"
android:label="#string/notifications"
android:screenOrientation="portrait"
android:theme="#style/WhiteActionBarTheme" />
<activity
android:name=".ViewNotificationActivity"
android:label="#string/notifications"
android:screenOrientation="portrait"
android:theme="#style/WhiteActionBarTheme" />
<activity
android:name=".NewFriendshipRequestActivity"
android:label="#string/notifications"
android:screenOrientation="portrait"
android:theme="#style/WhiteActionBarTheme" />
<activity
android:name=".TypeUserSelectionActivity"
android:screenOrientation="portrait"
android:theme="#style/WhiteActionBarTheme" />
<activity
android:name=".SelectTownActivity"
android:screenOrientation="portrait"
android:theme="#style/WhiteActionBarTheme" />
</application>
Add PendingIntent.FLAG_ONE_SHOT to your pending intent.
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_ONE_SHOT);
Also add android:exported="true" to the activity tag in manifest
Try this, it is working for me.
Intent notificationIntent = new Intent(this, MainActivity.class);
/*flags for resume activity*/
notificationIntent.setFlags( Intent.FLAG_ACTIVITY_SINGLE_TOP
|Intent.FLAG_ACTIVITY_CLEAR_TASK
|Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
/*for clear notification on click*/
builder.setAutoCancel(true);
I have some issue. When my app not in the background (removed from recent) everything works fine. But when my app is in the recent and then I open "ResponseActivity" via notification pending intent, on back click at "ResponseActivity" I getting to my MainActivity (Launcher activity).
I've added FLAG_ACTIVITY_NEW_TASK but it not seem to do it.
Intent intent = new Intent(this, ResponseActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_background)
.addAction(0, "Other", pendingIntent)
.setLargeIcon(getCircleBitmap(bitmap))
.setContentTitle(userDB.getName())
.setContentText(smallText)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setColor(getResources().getColor(R.color.colorPrimary))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
channel.setDescription("");
channel.enableLights(true);
channel.setLightColor(Color.RED);
channel.enableVibration(true);
mNotificationManager.createNotificationChannel(channel);
}
mNotificationManager.notify(1000, mBuilder.build());
Manifest:
<application
android:name=".ApplicationContextProvider"
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=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyFirebaseInstanceService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity android:name=".MainActivity" />
<activity android:name=".LoginActivity" />
<activity android:name="verification.MyVerifyPhoneActivity" />
<activity android:name=".ResponseActivity"
android:theme="#style/AppTheme2">
</activity>
</application>
If you want to be sure that only ResponseActivity is in the stack when you select the Notification, you can do this:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
instead of:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
I want to start a notification in current Activity which is ChatActivity. When I press the notification, I want to enter the ChatOneActivity.
However I don't want the current ChatActivity finished when I'm in the ChatOneActivity. Because I'm receiving the data and when I press the back button, I want to stay in the ChatActivity.
The point is I do not want the ChatActivity to finish, no matter which Activity I am currently in.
So what should I do?
Here is the code
private void showNotification(String id, String message) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(android.R.drawable.sym_action_email)
.setContentTitle("You have a new message")
.setContentText(message);
Intent intent = new Intent(ChatActivity.this,ChatOneActivity.class);
intent.putExtra("toId", id);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(ChatActivity.this);
stackBuilder.addParentStack(ChatOneActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, builder.build());
}
The problem is:
Now I enter the ChatOneActivityand when I press the back button, I return to the desktop. Means that the ChatActivity has already finished which I don't want.
Here is the mainfest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webmobilegroupchat"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.webmobilegroupchat.MainActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.webmobilegroupchat.ChatActivity"
android:label="#string/title_activity_chat" >
</activity>
<activity
android:name="com.example.webmobilegroupchat.ChatOneActivity"
android:label="#string/title_activity_chat_one" >
</activity>
<activity
android:name="com.example.webmobilegroupchat.SplashActivity"
android:label="#string/title_activity_splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Try this on ChatOneActivity activity .
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(getApplicationContext(),ChatActivity.Class);
startActivity(intent);
}
your code is looking fine for creating notification but you have to define parent activity in Manifest also to navigate back, stackBuilder.addParentStack assumes that you define it in Manifest as well for API 16+:
<activity android:name=".ChatOneActivity"
android:parentActivityName=".ChatActivity"
... />
for < API 16:
<activity android:name=".ChatOneActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ChatActivity" />
I have two Activities,ChatActivity and ChatOneActivity
I want to build a notification in the ChatActivity,and when I press the notification, I found the onDestroy() method is called. Why? I can understand why the onStop method getting called, because the view is no longer visible. But onDestroy()? Why? I didn't finish the ChatActivity!
How do I prevent it? I just want the onStop method to get called, I don't want the Activity to get killed.
Here is how I build the notification
private void showNotification(String id, String message) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getApplicationContext())
.setSmallIcon(android.R.drawable.sym_action_email)
.setContentTitle("You have a new message")
.setContentText(message);
Intent intent = new Intent(ChatActivity.this, ChatOneActivity.class);
intent.putExtra("toId", id);
TaskStackBuilder stackBuilder = TaskStackBuilder
.create(ChatActivity.this);
stackBuilder.addParentStack(ChatOneActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, builder.build());
}
And also my manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webmobilegroupchat"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.webmobilegroupchat.MainActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.webmobilegroupchat.ChatActivity"
android:label="#string/title_activity_chat" >
</activity>
<activity
android:name="com.example.webmobilegroupchat.ChatOneActivity"
android:label="#string/title_activity_chat_one" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.webmobilegroupchat.ChatActivity" />
</activity>
<activity
android:name="com.example.webmobilegroupchat.SplashActivity"
android:label="#string/title_activity_splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Because you called this code:
TaskStackBuilder stackBuilder = TaskStackBuilder.create(ChatActivity.this);
stackBuilder.addParentStack(ChatOneActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_CANCEL_CURRENT);
TaskStackBuilder.create will return a new TaskStackBuilder for launching a fresh task stack consisting of a series of activities.
Replacing them with a regular pendingintent like this solved it:
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
I created a notification setup in my application.when the user clicks the notification an activity Coding is suppose to open.But it isn't.When i checked the phone log(in the console of the android studio) it has some thing like this in it:
10-19 19:18:14.598 888-1437/? W/ActivityManager: Permission Denial: starting Intent { flg=0x1000c000 cmp=com.defcomdevs.invento16/.Coding bnds=[0,874][1080,1060] } from null (pid=-1, uid=10169) not exported from uid 10185
i don't understand what that is?
my code for notification is:
public class AlarmReceiver extends BroadcastReceiver {
static int notifyId=1;
#Override
public void onReceive(Context context, Intent intent) {
//Toast.makeText(context,"Alarm has been set",Toast.LENGTH_SHORT).show();
NotificationCompat.Builder mNotify=new NotificationCompat.Builder(context);
mNotify.setSmallIcon(R.drawable.index);
mNotify.setContentTitle("Coding");
mNotify.setContentText("INVENTO: Coding competition is going to be conducted today.");
Intent resultIntent=new Intent(context,Coding.class); //activity to open up when user clicks the notification
TaskStackBuilder stackBuilder=TaskStackBuilder.create(context);
stackBuilder.addParentStack(Coding.class); //add the to-be-displayed activity to the top of stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mNotify.setContentIntent(resultPendingIntent);
NotificationManager notificationManager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notifyId, mNotify.build());
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(context, notification);
r.play();
//note: on click display activity is not working.
}
}
please help!!
My manifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.defcomdevs.invento16" >
<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=".AlarmActivity"
android:label="#string/title_activity_alarm"
android:theme="#style/AppTheme.NoActionBar" >
</activity>
<receiver
android:name=".AlarmReceiver"
android:process=":remote" />
<activity
android:name=".Registration"
android:label="#string/title_activity_registration"
android:theme="#style/AppTheme.NoActionBar" >
</activity>
<activity
android:name=".Coding"
android:label="#string/title_activity_coding"
android:theme="#style/AppTheme.NoActionBar" >
</activity>
</application>
Add android:exported="true" to your .Coding activity tag in the manifest.xmlfile. Though have in mind that this allows other applications to start your activity.