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.
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);
One of the requirements in the app that I am developing is "show something on the screen on event." Basically, I made a background service and in that, I catch the screen on intent and pass it to a BroadcastReceiver. Then, in the receiver, I open an activity and "show something". However, on Android 7-8. I think the background service get killed over a night because the next day my service is not catching anything and my receiver is not receiving. I even ask for the
<uses-permission
android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
permission, and override it, but the service still gets killed. You can find my Service class, Receiver class, and manifest below.
Service class
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
public class Services extends Service {
private static final String TAG = "Services";
private BroadcastReceiver sReceiver;
public static final String ALARM_ALERT_ACTION = "com.android.deskclock.ALARM_ALERT";
public IBinder onBind(Intent arg) {
return null;
}
public int onStartCommand(Intent intent, int flag, int startIs) {
Log.i(TAG, "onStartCommand: " + this.toString() + " sReceiver = " + sReceiver);
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(ALARM_ALERT_ACTION);
sReceiver = new Receivers(new Handler());
registerReceiver(sReceiver, filter);
if(intent != null){
LUtil.appendToLogs("Service onStartCommand " + intent.getAction() + " intent catched.");
}
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(sReceiver);
}
}
Receiver class:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Handler;
import android.util.Log;
import com.mytoz.app.object.CommercialData;
import java.io.File;
import java.util.ArrayList;
public class Receivers extends BroadcastReceiver {
private static final String TAG = "Receivers";
private final Handler handler;
public Receivers(Handler handler) {
this.handler = handler;
}
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive: id = " + this.toString());
Log.i(TAG, "intent.getAction() " + intent.getAction());
LUtil.appendToLogs("onReceive " + intent.getAction() + " intent passed to receiver.");
if(LUtil.isPowerOff){
LUtil.isPowerOff = false;
return;
}
if (intent.getAction() != null) {
if(intent.getAction().equals(Services.ALARM_ALERT_ACTION)){
LUtil.lastAlarmAlert = System.currentTimeMillis();
Log.i(TAG, "onReceive: LUtil.lastAlarmAlert = " + LUtil.lastAlarmAlert);
return;
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
final int mode = am.getMode();
if (AudioManager.MODE_IN_CALL == mode) {
Log.i(TAG, "onReceive: in call");
// device is in a telephony call
} else if (AudioManager.MODE_IN_COMMUNICATION == mode) {
Log.i(TAG, "onReceive: commuunication");
// device is in communiation mode, i.e. in a VoIP or video call
} else if (AudioManager.MODE_RINGTONE == mode) {
Log.i(TAG, "onReceive: ringtone");
// device is in ringing mode, some incoming is being signalled
} else {
// SHOW SOMETHING
}
} else {
}
}
}
}
My 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="">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission
android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<application xmlns:tools="http://schemas.android.com/tools"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="#mipmap/app_logo"
android:label="#string/app_name"
android:largeHeap="true"
android:roundIcon="#mipmap/app_logo"
android:screenOrientation="portrait"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:replace="android:hardwareAccelerated"
android:name=".Mytoz">
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/facebook_app_id" />
<meta-data
android:name="firebase_crashlytics_collection_enabled"
android:value="false" />
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
<service android:name=".Services" />
<service
android:exported="false"
android:name=".FirebaseJobService">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
</intent-filter>
</service>
<service
android:name=".PushHandleService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<receiver
android:enabled="true"
android:exported="true"
android:name=".BootCompletedReceiver"
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>
<receiver
android:enabled="true"
android:exported="true"
android:name=".UpdateReceiver">
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
<activity
android:name=".SplashScreen"
android:theme="#style/SplashTheme" 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=".networking.CrashReportHandle" />
<activity android:name=".LoginActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".MainActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".CommercialVideo" android:taskAffinity=".CommercialVideo" android:excludeFromRecents="true" android:configChanges="orientation"
android:screenOrientation="portrait" android:launchMode="singleTop"/>
<activity android:name=".ShopActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".InterestActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".SignUpActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateVisible|adjustResize" />
<activity android:name=".MyAdsActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".SignUpThirdActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".SignUpSecondActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".RegisterActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".SettingsFirstActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".SettingsSecondActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".SettingsThirdActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".ItemDetailActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".CheckoutActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".DeliveryActivity" android:configChanges="orientation"
android:screenOrientation="portrait" android:windowSoftInputMode="stateVisible|adjustResize" />
<activity
android:configChanges="orientation"
android:screenOrientation="portrait"
android:name="com.braintreepayments.api.BraintreeBrowserSwitchActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="${applicationId}.braintree" />
</intent-filter>
</activity>
<activity android:name=".PaymentActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".CreditCardActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".OfferActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".ConfirmationActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".AdsSecondActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".CustomAdsVideoActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".OrderActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".OrderDetailsActivity" android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".SmartyAdsActivity"
android:configChanges="orientation|keyboardHidden|screenSize" />
</application>
</manifest>
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.
Before my application was working fine. I just created a new class (within the same package) and also declared it in the manifest, still I get a ActivityNotFoundException and sometimes NoClassDefFoundException.
TestActivity.java
public class TestActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.order_summary);
}
}
Activity_NewOrder.java
public class Activity_NewOrder extends Activity
try {
//Also tried Intent intent = new Intent(Activity_NewOrder.this, TestActivity.class);
Intent intent = new Intent(getApplicationContext(), TestActivity.class);
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mypackage">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".Activities.Activity_OrderSummaryNew"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape" />
<activity
android:name=".Activities.Activity_Login"
android:configChanges="orientation"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activities.Activity_Orders"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape" />
<activity
android:name=".Activities.Activity_NewOrder"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape" />
<activity
android:name=".Activities.Activity_OrderHistory"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape" />
<activity android:name=".Activities.TestActivity" />
</application>
</manifest>
Hello i am new to android and i written a small app for "Simple web Browser".
but my browser is unable to open the page and it's showing error like no network available(Web Page not available) even wifi is on and normal browser is working on that device.
please check the code below::
SimpleBrowser.java
package com.thenewboston.travis;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
public class SimpleBrowser extends Activity implements OnClickListener{
Button bGo,bBack,bForward,bHistory,bRefresh;
EditText et;
WebView wb;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.simplebrowser);
bGo = (Button)findViewById(R.id.go);
bBack = (Button)findViewById(R.id.bBack);
bForward = (Button)findViewById(R.id.bForward);
bHistory = (Button)findViewById(R.id.bHistory);
bRefresh = (Button)findViewById(R.id.bRefresh);
et = (EditText)findViewById(R.id.editAddress);
wb = (WebView)findViewById(R.id.webView);
wb.setWebViewClient(new ourViewClient());
bGo.setOnClickListener(this);
bBack.setOnClickListener(this);
bForward.setOnClickListener(this);
bHistory.setOnClickListener(this);
bRefresh.setOnClickListener(this);
//wb.loadUrl("http://www.gmail.com");
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId())
{
case R.id.go:
String website = et.getText().toString();
wb.loadUrl(website);
break;
case R.id.bBack:
if(wb.canGoBack())
wb.goBack();
break;
case R.id.bForward:
if(wb.canGoForward())
wb.goForward();
break;
case R.id.bHistory:
wb.clearHistory();
break;
case R.id.bRefresh:
wb.reload();
break;
}
}
}
ourViewClient.java
package com.thenewboston.travis;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class ourViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
and ....
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.thenewboston.travis"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="KCRaju"
android:theme="#style/AppTheme">
<activity
android:name="com.thenewboston.travis.Splash"
android:label="KCRaju"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".StartingPoint"
android:label="KCRaju" >
<intent-filter>
<action android:name="com.thenewboston.travis.STARTINGPOINT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:label="KCRaju" >
<intent-filter>
<action android:name="com.thenewboston.travis.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".prefs"
android:label="KCRaju" >
<intent-filter>
<action android:name="com.thenewboston.travis.prefs" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".About"
android:label="KCRaju"
android:theme="#android:style/Theme.Dialog" >
<intent-filter>
<action android:name="com.thenewboston.travis.About" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".TextPlay" android:label="#string/app_name" >
</activity>
<activity android:name=".Email" android:label="#string/app_name" >
</activity>
<activity android:name=".Camera" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".Data" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".GFX" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".GFXSurface" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".SoundStuff" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".Slider" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".Memory" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".OpenedClass" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".Tabs" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
<activity android:name=".SimpleBrowser" android:label="#string/app_name" android:screenOrientation="portrait" >
</activity>
</application>
</manifest>
We need to provide url with https as suffix to loadUrl
In your code :
wb.loadUrl(website);
What you passed website.check the string it is valid website.Copyt the string and paste in android phone's browser then check it is display or not