I can receive gcm notification in all the devices. But when i click the notification then it is not navigating to corresponding activity what I have given in the Intent. I did not set any flags for the Intent.
It's working fine in all other devices.
This is my code.
Intent notificationIntent = new Intent(GcmIntentService.this, NotificationActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(GcmIntentService.this, 0, notificationIntent, 0);
mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.gcm)
.setContentTitle("New messages")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setDefaults(Notification.DEFAULT_SOUND)
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mBuilder.setAutoCancel(true);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
My manifest
<receiver
android:name="mellotv.com.gcm.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.gcm" />
</intent-filter>
</receiver>
I have checked the device api and it is 19.
Could some one help me to solve this problem?
Related
I'm using firebase cloud function for showing notification in my app and that working perfectly only issue I'm facing is when app is closed or in background notification doesn't show pop up.
Heads up notifications are used in app and I do get the notification but notification doesn't pop up as it do while app is in foreground.I have set the Priority to high for both channel and notification but still it doesn't work.
My Service Class :
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
Intent intent = new Intent(this, WebViewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "Default";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.app_logo)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true).setContentIntent(pendingIntent)
.setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE)
.setPriority(Notification.PRIORITY_MAX);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_HIGH);
channel.setShowBadge(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
manager.createNotificationChannel(channel);
}
manager.notify(0, builder.build());
}
Manifest
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name="com.noderon.riscalert.WebViewActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.noderon.riscalert.MainActivity">
</activity>
<service
android:name=".NotificationService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</application>
Any help would be appreciated. Thanku
As you are already setting the Priorities it seems like you are not adding the metadata for the notification and when the app is in the background it won't get any data that's why it only showing the pop up in the foreground
add these metadata in your code:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/ic_stat_ic_notification" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/colorAccent" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="#string/default_notification_channel_id" />
Also, you can see Google Cloud Messaging documentation for more details
Happy coding
I have faced the same issue. I have followed the official documentation from the firebase and now notifications are working fine. Follow this link
.
Have you created FirebaseMessage Instance() in your Activity?
I am new to learning flutter. My flutter app has a native android background service on kotlin where I have a socket running and whenever the socket listens to something I generate a notification using NotificationCompat.Builder.
But I am unable to bring the Flutter app back to foreground when ever the user taps on the notification.
I have tried creating a pendingintent and passing it to notificationbuilder setContentIntent() but nothing happens on tap.
shownotification function on Native background service:
fun shownotification (){
packageNamer = this.getPackageName()
launchIntent = this.getPackageManager().getLaunchIntentForPackage(packageName)
var className = launchIntent?.getComponent()!!.className.javaClass
intent.action = "SELECT_NOTIFICATION"
intent.setClass(this,className)
pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT)
var builder = NotificationCompat.Builder(this, "notifications")
.setOngoing(true)
.setContentIntent(pendingIntent)
.setFullScreenIntent(pendingIntent, true)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentTitle("My notification")
.setContentText("Much longer text that cannot fit one line...")
.setStyle(NotificationCompat.BigTextStyle()
.bigText("Much longer text that cannot fit one line..."))
.setPriority(NotificationCompat.PRIORITY_HIGH)
manager?.notify(456,builder.build())
}
AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name=".MyApplication"
android:label="bizzyb_demo"
android:icon="#mipmap/ic_launcher">
<service android:name=".MyService" android:exported="true" android:enabled="true"/>
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="#style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- This keeps the window background of the activity showing
until Flutter renders its first frame. It can be removed if
there is no splash screen (such as the default splash screen
defined in #style/LaunchTheme). -->
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
What am I doing wrong here. I believe I am missing something very obvious here as I am new to mobile apps development.
Your expert feedbacks will be appreciated.
Thanks
You need to include click_action: FLUTTER_NOTIFICATION_CLICK as a "Custom data" key-value-pair in the notification payload.
Bundle bundle = new Bundle();
bundle.putString(Constants.EXAM_ID,String.valueOf(lectureDownloadStatus.getExamId()));
var builder = NotificationCompat.Builder(this, "notifications")
.setOngoing(true)
.setContentIntent(pendingIntent)
.setFullScreenIntent(pendingIntent, true)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentTitle("My notification")
.setContentText("Much longer text that cannot fit one line...")
.setStyle(NotificationCompat.BigTextStyle()
.bigText("Much longer text that cannot fit one line..."))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setExtras(bundle) // Set the bundle
I create a wearable compatible notification with a second page which should embedd a custom activity (on the wearable).
Here is the relevant part of the Manifest from the wearable app:
<activity android:name=".Test"
android:label="aaa"
android:allowEmbedded="true"
android:taskAffinity=""
android:theme="#android:style/Theme.DeviceDefault.Light">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Here is my related android code from the phone:
Intent notificationIntent = new Intent();
notificationIntent.setClassName(BuildConfig.APPLICATION_ID, BuildConfig.APPLICATION_ID + ".wear.Main");
PendingIntent notificationPendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap bg = BitmapFactory.decodeResource(getResources(), R.drawable.bg_wearable);
Notification test = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText("Message")
.extend(new NotificationCompat.WearableExtender()
.setBackground(bg)
.addPage(new NotificationCompat.Builder(this)
.setContentTitle("Page 1")
.extend(new NotificationCompat.WearableExtender()
.setDisplayIntent(notificationPendingIntent)
.setCustomSizePreset(NotificationCompat.WearableExtender.SIZE_FULL_SCREEN)
).build()
)
)
.build();
NotificationManagerCompat.from(this).notify(12345, test);
Do you have any idea why this don't work on the phone, but on the wearable itself?
You can only create and issue custom notifications with embedded activities from the wearable itself.
From the docs:
You can only create and issue custom notifications on the wearable,
and the system does not sync these notifications to the handheld.
Source
I have a pair applications where I use a GCMBroadcastReceiver and a GCMIntentService to listen for google cloud messaging messages and then send a notification to Android's tray via NotificationManager. Pretty standard code, as far as I know I'm not doing anything strange here.
And the code works, most all devices show my notifications (both) , each application shows a similar notification, with a different icon.
The problem is, the Galaxy Tab 10 is only showing one of them, and the other not. And I can't put my finger on what could be causing the difference.
Specially since the code is so simple.
here is the code where I make the notification:
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 1,
new Intent(this, HomeActivity.class), 0);
Resources res = getResources();
Bitmap bm = BitmapFactory.decodeResource(res, R.drawable.notifier_app_launcher);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setLargeIcon(bm)
.setSmallIcon(R.drawable.app_icon_notifier)
.setContentTitle(getString(R.string.title))
.setNumber(number)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Also, this is how I define my receiver:
<receiver android:name="com.company.mobile.app.GcmBroadcastReceiver" android:exported="true" android:process=":remote" android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.company.mobile.app" />
</intent-filter>
Is it a known error of that model? Has anyone experienced this before? am I missing something in the code? All feedback is welcome.
I'm working with push notifications. Receiving and displaying notifications is currently working, however the on-click event isn't acting as it should.
int NOTIFICATION_ID = 1593;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notification, title, System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, MyMainActivity.class);
notificationIntent.putExtra("test", "test");
notificationIntent.setAction("load_notifications");
PendingIntent intentNotification = PendingIntent.getActivity(this, 999, notificationIntent, 0);
notification.setLatestEventInfo(this, title, body, intentNotification);
notificationManager.notify(NOTIFICATION_ID, notification);
That's how I display my notification and it also includes starting/resuming my main activity.
However, the putExtra("test", "test") and the setAction seem to have no effect if I click on a notification while I'm in the app. If my app isn't working, launching it from the notification works fine.
My onResume function at my main activity:
public void onResume() {
super.onResume();
if(this.getIntent().getAction().equalsIgnoreCase("load_notifications")) {
//Do stuff
}
}
My app config is as following:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".Activity"
android:label="#string/app_name"
android:configChanges="orientation"
android:launchMode="singleInstance"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Push notifications -->
<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.app.android" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
the answer maybe be here : Action buttons are not shown in notification
Notification Actions were introduced in API 16.
This means that you should be setting your target SDK to 16 or above.
However, only Jelly Bean and above can take advantage of the Notification Actions - make sure to test this feature on those platforms.
NotificationCompat.Builder takes care of generating a Notification compatible with whatever API it is running on, so keep using it if you are going to be supporting Ice Cream Sandwich and older. If not, simply using Notification.Builder will suffice (after changing the target SDK of course).