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.
Related
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 want to create a notification from my app when a new picture is taken from the camera app. I want to achieve this when my app is not running. I am using Broadcast receiver to do this.
Here is my Code...
In Android Manifest..
<receiver
android:name=".receivers.CameraEventReceiver"
android:label="CameraEventReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.hardware.action.NEW_PICTURE" />
<data android:mimeType="image/*" />
</intent-filter>
</receiver>
In my receiver class
public class CameraEventReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel("CHANNEL_ID", "my channel name", NotificationManager.IMPORTANCE_HIGH);
manager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "CHANNEL_ID")
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Picture Taken")
.setContentText("here is the uri : "+intent.getData())
.setDefaults(NotificationCompat.DEFAULT_VIBRATE)
.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
}
manager.notify(222, builder.build());
}
}
It is working fine for Android 6.0 when the app is running...
But it is not working for newer versions. What can I do to achieve this?
I want it to support for all devices with android versions greater than 4.1 (JELLY_BEAN)
Thanks in advance
But it is not working for newer versions.
Correct. Those broadcasts are no longer supported. See the Android 7.0 release notes.
What can I do to achieve this?
Take the picture yourself, whether using ACTION_IMAGE_CAPTURE, the camera APIs, or libraries (e.g., Fotoapparat, CameraKit-Android).
There is no direct replacement for that broadcast, and there was no requirement for camera apps to trigger that broadcast anyway.
You could use JobScheduler and addTriggerContentUri() to monitor the MediaStore for changes. However, I do not know how you would limit it to only pictures taken by a camera app.
I have implemented when new SMS arrived and i was facing the same issue. I have put below lines in my manifest file.
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.INPUT_METHOD_CHANGED" />
</intent-filter>
I am using FCM cloud messaging and it is working fine when the app is in the background but not killed or when the app is running.
Once the app is stopped or killed from recent apps it doesn't receive any notification.
Even it doesn't receive the old messages once the app is started.
code for onMessageReceived()
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
String myCustomKey = data.get("Nick");
String myCustomKey1 = data.get("Room");
testNotification(myCustomKey,myCustomKey1);
}
private void testNotification(String message,String title) {
Intent intent = new Intent(this, ChatActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.sham_icon)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
server side code for sending notification
{
"to" : "token-key",
"data" : {
"title":"title",
"body":"body",
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
}
manifest code for firebase services and notification
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/narendramodi_icon" />
android:resource="#drawable/sham_icon" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/accent" />
<service android:name=".firebase.MyFirebaseMessagingService"
android:exported="false">
<intent-filter android:priority="10000">
<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>
Any help is appreciated.
This happens because on some devices system doesn't allow starting apps or app services by other apps, For FCM notifications Google Play services starts your app service if your app has been killed, when they are killed (ex. Xiaomi phones with MIUI).
Use your app server and FCM server API: Set the data key only. Can be either collapsible or non-collapsible.
If you are sending the notification from console, then use the particular key to send the notification from the Advanced Option features.
if (getIntent().getExtras() != null) {
// Call your NotificationActivity here..
Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
startActivity(intent);
}
The Android framework advises that apps that have been stopped (not in background) should not be started without explicit user interaction.
FCM follows this recommendation and thus does not deliver messages to stopped apps.
For some phones,Once the app is stopped or killed from recent apps, it was completely stopped by the OS. this can save battery but fcm won't work.
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?
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