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);
Related
I have added Firebase Cloud Messaging to my app and I setuped Firebase messaging services. And it was working fine and app was receiving notification even when the app was not running on background. But from last 2 days App is not receiving any notification. I don't know what happened with my code.
FcmMessagingService.java
import android.annotation.SuppressLint;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FcmMessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size()>0){
String title,message,img_url;
title = remoteMessage.getData().get("title");
message = remoteMessage.getData().get("message");
img_url = remoteMessage.getData().get("img_url");
Intent intent = new Intent(this, Notification.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.app_icon_round);
Uri sounduri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String id = "MY_CH_ID";
CharSequence name = "Product";
String description = "Notifications regarding our products";
int importance = NotificationManager.IMPORTANCE_MAX;
#SuppressLint("WrongConstant")
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
mChannel.setDescription(description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.setSound(sounduri,attributes);
notificationManager.createNotificationChannel(mChannel);
}
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MY_CH_ID");
builder.setContentTitle(title);
builder.setSubText(message);
builder.setContentIntent(pendingIntent);
builder.setSound(sounduri);
builder.setSmallIcon(R.drawable.fcmicon);
builder.setLargeIcon(rawBitmap);
builder.setChannelId("MY_CH_ID");
ImageRequest imageRequest = new ImageRequest(img_url, new Response.Listener<Bitmap>() {
#Override
public void onResponse(Bitmap response) {
builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(response));
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0,builder.build());
}
}, 0, 0, null, Bitmap.Config.RGB_565, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
MySingleton.getmInstance(this).addToRequestQue(imageRequest);
}
}
}
In my main activity I have subscribed to a topic.
FirebaseMessaging.getInstance().subscribeToTopic("PARETHUMUKAL_CHURCH");
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.parethumukal">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/app_icon_round"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:usesCleartextTraffic="true">
<activity android:name=".Archived"
android:configChanges="orientation"
android:screenOrientation="portrait" ></activity>
<activity android:name=".Ebook"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".Prayer"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".Churchtiming"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Forgotpassword"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Privacypolicy"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Directoryquick"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Churchinside"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Extras"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Shrine"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".Youtubeplayer" />
<activity
android:name=".Video"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Driveactivity"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Audios"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Images"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Profileregistration"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Registration"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Milandetails"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Ambulance"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Milan"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Feast"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Priest"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Churchadmin"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Youthassociation"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Vanithasamajam"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Familyunit"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Sundayschool"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Leaders"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".History"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".Livestream" />
<activity
android:name=".Notification"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Myprofile"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Quicklinks"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Services"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Directory"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Gallery"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Groups"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Administration"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Church"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Aboutus"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".WelcomeActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Homewindow"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Login"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Splashscreen"
android:configChanges="orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".FcmMessagingService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
</application>
</manifest>
build.gradle implementation
implementation 'com.google.firebase:firebase-messaging:19.0.1'
I dont know what is the problem.
Everything is fine on your end. You just need to send Firebase Notification Messages see here.
As you can see there is Notification JSON object and it includes title and body keys which will be displayed in the notification title.
Sample:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}
}
Don't forget to Replace token with your FCM device token.
I've changed my Launcher Activity instead of .MainActivity. The Firebase push notifications service doesn't work when my app is in the background but it works fine when the app is in the foreground.
I've added a WelcomeSlider in my app that's why I have to keep Welcome slider as the Launcher Activity. I checked that if I change the launcher activity to NotifyActivity then it works fine again.
here is my AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".WelcomeActivity"
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=".MainActivity"
android:configChanges="orientation|screenSize"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Vlog"
android:parentActivityName=".MainActivity" />
<activity android:name=".NotifyActivity"></activity>
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
Here is myFirebaseInstanceIdService:
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
#Override
public void onTokenRefresh(){
String token = FirebaseInstanceId.getInstance().getToken();
Log.d("TOKEN",token);
}
}
Here is myFirebaseMessagingService:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Intent intent = new Intent(this,NotifyActivity.class);
if(remoteMessage.getData().size()>0){
String url = remoteMessage.getData().get("url");
Bundle bundle = new Bundle();
bundle.putString("url",url);
intent.putExtras(bundle);
}
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(remoteMessage.getNotification().getTitle());
notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.mipmap.notifyicon);
Uri sound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder.setSound(sound);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
notificationBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
}
}
I've solved this problem by my own.I just keep the MainActivity as a launcher Activity from Android Manifest like
<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>
and post below code inside MainActivity onCreate for checking if its the first time launch or not!
//Check if it first time launching..
Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.getBoolean("isFirstRun", true);
if (isFirstRun) {
//show start activity
startActivity(new Intent(MainActivity.this, WelcomeActivity.class));
Toast.makeText(MainActivity.this, "First Run", Toast.LENGTH_LONG)
.show();
}
it worked for me! :) :)
I am trying to open an activity from the launcher activity when an FCM-generated notification gets clicked. The startActivity function does nothing even when the log statement written above gets executed. Following are some relevant files/functions, please tell why is the ConversationDetailActivity not starting:
The function which starts another activity from LoginActivity
void handleNotificationClick()
{
if (getIntent().getExtras() != null)
{
for (String key : getIntent().getExtras().keySet())
{
Object value = getIntent().getExtras().get(key);
System.out.println("----- pushNotif Key: " + key + " Value: " + value);
}
Intent intent;
if(getIntent().getStringExtra(Master.KEY_PUSH_NOTIFICATION_CATEGORY).equals(Master.KEY_PUSH_NOTIFICATION_CONVERSATION))
{
intent = new Intent(LoginActivity.this, ConversationDetailActivity.class);
}
else
{
intent = new Intent(LoginActivity.this, ApprovalDetailActivity.class);
if(getIntent().getStringExtra(Master.KEY_PUSH_NOTIFICATION_APPROVAL_TYPE).equals("I"))
intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_VERIFICATIONS);
else if(getIntent().getStringExtra(Master.KEY_PUSH_NOTIFICATION_APPROVAL_TYPE).equals("A"))
intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_APPROVALS);
else
intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_COMPLETED);
intent.putExtra(Master.KEY_IS_FROM_CONVERSATION, false);
}
intent.putExtra(Master.KEY_PUSH_NOTIFICATION_POST_ID , getIntent().getStringExtra(Master.KEY_PUSH_NOTIFICATION_POST_ID));
intent.putExtra(Master.KEY_IS_FROM_PUSH_NOTIFICATION, true);
intent.putExtra(Master.KEY_POSITION, 0);
System.out.println("----- starting new activity from handleNotificationClick");
LoginActivity.this.startActivity(intent);
LoginActivity.this.finish();
}
}
The sendNotification function from which the notification is generated
private void sendNotification(String messageBody)
{
Intent intent;
System.out.println("----message body: " + messageBody);
if(notificationBundle.getCategory().equalsIgnoreCase(Master.KEY_PUSH_NOTIFICATION_CONVERSATION))
{
intent = new Intent(this, ConversationDetailActivity.class);
}
else
{
intent = new Intent(this, ApprovalDetailActivity.class);
if(notificationBundle.getApprovalType().equals("I"))
intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_VERIFICATIONS);
else if(notificationBundle.getApprovalType().equals("A"))
intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_APPROVALS);
else
intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_COMPLETED);
intent.putExtra(Master.KEY_IS_FROM_CONVERSATION, false);
}
intent.putExtra(Master.KEY_PUSH_NOTIFICATION_POST_ID , notificationBundle.getPostID());
intent.putExtra(Master.KEY_IS_FROM_PUSH_NOTIFICATION, true);
intent.putExtra(Master.KEY_POSITION, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.mnet_icon)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int random = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
notificationManager.notify(random, notificationBuilder.build());
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vishesh.test">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:name=".MnetApplication"
android:allowBackup="true"
android:icon="#drawable/mnet_icon"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".activities.LoginActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:launchMode="singleTask"
android:theme="#style/Theme.AppCompat.Light.NoActionBar.FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_main"
android:launchMode="singleTask"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".activities.ConversationDetailActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_conversation_detail"
android:parentActivityName=".activities.MainActivity"
android:theme="#style/AppTheme.NoActionBar"
android:launchMode="singleTask"
android:windowSoftInputMode="stateHidden|adjustResize">
<intent-filter>
<action android:name=".activities.ConversationDetailActivity" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="mnet.mediaware.com.m_net.activities.MainActivity" />
</activity>
<activity
android:name=".activities.ApprovalDetailActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_approval_detail"
android:parentActivityName=".activities.MainActivity"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustResize">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="mnet.mediaware.com.m_net.activities.MainActivity" />
</activity>
<activity
android:name=".activities.NewConversationActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_new_conversation"
android:parentActivityName=".activities.MainActivity"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustResize">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="mnet.mediaware.com.m_net.activities.MainActivity" />
</activity>
<activity
android:name=".activities.NotificationActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_notification"
android:parentActivityName=".activities.MainActivity"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustResize">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="mnet.mediaware.com.m_net.activities.MainActivity" />
</activity>
<activity
android:name=".activities.ProfileActivity"
android:label="#string/title_activity_profile"
android:configChanges="orientation|keyboardHidden|screenSize"
android:parentActivityName=".activities.MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="mnet.mediaware.com.m_net.activities.MainActivity" />
</activity>
<service
android:name=".utils.firebase.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".utils.firebase.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
Try Adding android:exported="true" to the activity in AndroidManifest.xml
First finish Login activity, and then call start activity.
LoginActivity.this.finish();
LoginActivity.this.startActivity(intent);
In your manifest, define your class which you want to open like this
<activity
android:name=".activities.ApprovalDetailActivity"
android:exported="true"
android:screenOrientation="portrait"
android:theme="#style/MyMaterialTheme"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Nothing works for me except this one. The thing work for me is simple. Make sure you add this in the activity that you want to open directly.
<intent-filter>
<action android:name="MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
And from the push notification you must add a new payload: click_action
it will looks like this -
"notification": {
"title": "hello",
"body": "test message",
"click_action": "MAIN_ACTIVITY"
},
Note: You can name it as you want MAIN_ACTIVITY but must be same in both place.
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.
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());