clicking on notification to launch activity (email) app fails - android

The purpose of my app is to send an email at a predetermined time based on a schedule set with a broadcast receiver alarm using the users built in email configuration. The To: and Subject are already filled out. All the user has to do, is click on the notification when the alarm goes off, and it should prompt the user via an intent what email program to use via my SendMail.class.
If I call the class directly by dropping it into my "MainActivity onCreate it works. If I place it in my Intent definition where I build my notification it never appears to get called. This is the meat of my problem. I need the intent create chooser to pop up with its list of valid email programs when the user clicks on the notification and its just not working. Any help would be greatly appreciated! :)
Coincidentally, I also have a preferences fragment to hold the settings of my app, and if I replace the call to SendMail.class to SetPreferenceActivity, then the notification works as expected (i.e. it successfully calls another class when the user clicks on it).
Here is the code for my notification manager method:
public void createNotification(View view) {
Context context = getApplicationContext();
SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String default_subject = mySharedPreferences.getString("default_subject", "");
default_subject = default_subject + android.text.format.DateFormat.format("E MM-dd", new java.util.Date());
Intent intent = new Intent(this, SendMail.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
Notification noti = new Notification.Builder(this)
.setContentTitle("Time to Send the Email")
.setContentText("Date: "+default_subject)
.setContentIntent(pIntent)
.build();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
And here is what is going on in the SendMail.class
public class SendMail extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sendMail();
}
public void sendMail() {
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto", "somebody#gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Some Subject Text");
startActivity(Intent.createChooser(emailIntent, "Send email..."));
}
}
Here is the contents of my Manifest
<.....snipped header>
<application
android:allowBackup="true"
android:icon="#drawable/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=".SetPreferenceActivity"
android:label="#string/app_name" >
</activity>
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true"
android:label="MintBootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.BOOT_COMPLETED" >
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</action>
</intent-filter>
</receiver>
</application>

Related

Launcher Activity changed that's why FCM Push Notification doesn't work while app in background

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'm not able to open specified activity via notification

I'm using Firebase notifications to send notifications in my Android App. I want to achieve following:
When user clicks notification, activity of my choice should open and activity should perform some actions automatically which I'll define like loading specific url within webview, etc.
What I did?
In Firebase console, under custom data, I set key as click_action. And value as ttdemo and sent notifications.
In my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".aboutmain" />
<activity android:name=".Timetable">
<intent-filter>
<action android:name="ttdemo"/>
</intent-filter>
</activity>
<activity android:name=".Results" />
<activity android:name=".timetable_downloads" />
<activity android:name=".result_downloads" />
<activity android:name=".eforms" />
<activity android:name=".write" />
<activity android:name=".qpapers" />
<activity android:name=".november2014" />
<activity android:name=".november_2014_downloads" />
<activity android:name=".april_2015" />
<activity android:name=".november_2015" />
<activity android:name=".april_2016"></activity>
</application>
Current scenario:
I'm getting notifications but Timetable activity is not opened.
Instead, the main activity opens.
Try like blow code when your onMessageReceived method called.
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
if (data.containsKey("click_action")) {
String className = data.get("click_action");
Class cls;
try {
cls = Class.forName(className);
Intent i = new Intent(context, cls);
//i.putExtras(extras); // put extras if you required
context.startActivity(i);
}catch(ClassNotFoundException e){
//means you made a wrong input in firebase console
}
}
}
To send a notification with firebase console, put a key-value-pair as custom data like this:
Key: click_action
Value: <fully qualified class name of your activity>
You need to pass panding intent to notification in the onMessageReceived(RemoteMessage remoteMessage) method of class FirebaseMessagingService
Intent resultIntent = new Intent(FirebaseMessagingService.this, MobilActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0,
resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);
and set to notification object as
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setContentIntent(resultPendingIntent)
and method like
private void showSmallNotification(NotificationCompat.Builder mBuilder, int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent, Uri alarmSound) {
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(inboxStyle)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.growth_indus)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(AppConstants.getParamInt(AppConstants.GlobalParamEnum.NOTIFICATION_ID), notification);
}
Finally, I achieved the objective!
This solution worked perfectly for me! http://androidbash.com/firebase-push-notification-android/

Cannot use notification in android? [duplicate]

This question already has an answer here:
Android show a notification not working
(1 answer)
Closed 6 years ago.
I am sending a notificatioin from this Broadcast reciver which is triiggered by an alarm manager to send sms but i cant see any notification.
I also tried to include sent intent and delivery intent but cant use registerReciever here.
Here is my Code.
Broadcast Reciever
public class MyReceiver extends BroadcastReceiver {
String SENT="sent";
public final String tag="com.example.pritesh.smstimer";
public MyReceiver() {
}
//#Override
public void onReceive(Context context, Intent intent) {
Log.i(tag,"Sending");
NotificationManager notificationManager= (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent1=new Intent(context,Time_Picker.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent1,0);
Notification.Builder notification=new Notification.Builder(context)
.setContentTitle("SMS SENT")
.setContentText("Your SMS has Been Sent")
.setContentIntent(pendingIntent);
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(Time_Picker.Phone,null, Time_Picker.Message, null, null);
notificationManager.notify(0,notification.build());
Toast.makeText(context, "Sms Sent", Toast.LENGTH_LONG).show();
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="#drawable/favicon"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".Time_Picker"
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=".noti"/>
<activity android:name=".MainActivity">
</activity>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true"/>
</application>
Have you registered the receiver in your androidmanifest.xml or called registerReceiver to do the same? I don't think it will work until you do.
See here:https://developer.android.com/guide/topics/manifest/receiver-element.html

Activity doesn't start when clicked on the notification

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.

Can anyone see why this Intent isnt being picked up?

//Create intent
notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
note = new Notification(android.R.drawable.btn_star_big_on, contentText, System.currentTimeMillis() );
Intent notificationIntent = new Intent();
notificationIntent.putExtra("aff_id",aff_id);
notificationIntent.setAction("com.mindfsck.PossAff.aff");
notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
contentIntent = PendingIntent.getActivity(ctx, aff_id, notificationIntent, 0);
note.setLatestEventInfo(ctx, title, aff_id + contentText, contentIntent);
notificationManager.notify(aff_id,note);
//Pickup intent
package com.mindfsck.PossAff;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.app.PendingIntent;
public class PosAffIntentReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.mindfsck.PossAff.aff")) {
System.out.println("Picked up broadcast with aff");
context.startActivity(new Intent(context, com.mindfsck.PossAff.MainActivity.class));
}else if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
}
System.out.println("Picked up broadcast");
}
}
//Manifest
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:debuggable="true">
<activity android:name=".MainActivity"
android:label="#string/app_name" android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".PosAffIntentReceiver">
<intent-filter>
<!--<action android:name="com.mindfsck.PossAff.intent.action.aff"></action> -->
<action android:name="com.mindfsck.PossAff.aff"></action>
</intent-filter>
</receiver>
<receiver android:name=".NotificationAlarm">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
</application>
I see the following in the Debug monitor when I click the notification
03-04 11:00:24.652: INFO/ActivityManager(1296): Starting activity: Intent { act=com.mindfsck.PossAff.aff flg=0x200000 (has extras) }
but it never gets picked up
Missing the DEFAULT category in the intent filter in your manifest? (Assuming I understand what you mean with "never gets picked up" correctly).
I changed:
contentIntent = PendingIntent.getActivity(ctx, aff_id, notificationIntent, 0);
To
contentIntent = PendingIntent.getBroadcast(ctx, aff_id, notificationIntent, 0);

Categories

Resources