I am trying to remotely lock my phone when a gcm PUSH notification with a keyword arrives.
Inside the GcmIntentService class, within the onHandleIntent I have included
message = extras.getString("message");
if(message.equals("LOCK")){
gcmaction(message);
}
else{
sendNotification("Message: " + extras.getString("message"));
Log.i(TAG, "Message: " + extras.toString());
}
So if a message arrives saying LOCK, it should go to the gcmaction method. Otherwise go to the notification method.
private void gcmaction(String string) {
mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
//lock action
mDPM.resetPassword(Password,
DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
mDPM.lockNow();
}
sendNotification is correctly showing notfications but nothing happens when I send "LOCK" ... can someone tell me what I am missing ?
Thanks
Related
I will try to show personal single notification on my phone tray, but I can't rich, so help.
I am having an issue with FireBase Cloud Messaging in which I get the Token from the device and send the notification test through the Google Firebase notification console, however, the notification is never logged nor pushed to the android virtual device. The documentation for FCM is almost exactly the code that I have below and little else in the way of what else you would have to do to get push notifications working with firebase. I have gone through all of the setup information (build.gradle additions, Installing google play services, etc...) as specified in the documentation, but still do not have messages generating. What is wrong with the code that I am not receiving my push notifications to the logcat or the device? Please let me know any further information that would be helpful. Thanks.
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);
displayFirebaseRegId();
} else if (intent.getAction().equals(Config.PUSH_NOTIFICATION))
{
String message = intent.getStringExtra("message");
Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show();
txtMessage.setText(message);
}
}
};
displayFirebaseRegId();
}
private void displayFirebaseRegId() {
SharedPreferences pref = getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0);
String regId = pref.getString("regId", null);
Log.e(TAG, "Firebase reg id: " + regId);
if (!TextUtils.isEmpty(regId))
txtRegId.setText("Firebase Reg Id: " + regId);
else
txtRegId.setText("Firebase Reg Id is not received yet!");
}
#Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(Config.REGISTRATION_COMPLETE));
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(Config.PUSH_NOTIFICATION));
NotificationUtils.clearNotifications(getApplicationContext());
}
#Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
super.onPause();
}
And I will add lib of fire base messaging is:
compile 'com.google.firebase:firebase-messaging:11.0.4'
You don't need to subscribe inside the BroadcastReceiver you can just do it inside the onTokenRefresh method in the FirebaseInstanceIdService
You don't need to get the push notification in the BroadcastReceiver, you have to do it inside the onMessageReceive in the FirebaseMessagingService
FCM is extremely unreliable with emulators, simply use a real device, I have struggled with this and in some cases I even get the notification days later when opening the emulator for other projects, test this with real phones
I am trying to send push notification from FCM .But unable to receive it in my android device.
At first you can see in the image that it's not getting initialized but later the initialization is successful.
I am able to get the FCM registration id but unable to get the push message .
here is the screenshot:
OnReceive Method :
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// checking for type intent filter
if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
// gcm successfully registered
// now subscribe to `global` topic to receive app wide notifications
FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);
displayFirebaseRegId();
} else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
// new push notification is received
String message = intent.getStringExtra("message");
Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show();
txtMessage.setText(message);
}
}
};
I am using this example of AndroidHive and I have also tried to test it on different devices with different versions.
if anyone can give some idea then it will be very helpful to me.
I am a little bit confusing on integrating FCM (Firebase cloud messaging) push notification on my application.
Normally, Rather than the Intent services other services are not stopped anymore in the middle. I have created my message receiving service by extending to the FirebaseMessagingService as follow
public class MyFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage == null)
return;
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.e(TAG, "NotificationBean Body: " + remoteMessage.getNotification().getBody());
//This method is responsible for handling notification
handleNotification(remoteMessage.getNotification().getBody());
}
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
handleDataMessage(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}
}
and registered the service on manifest as follow:
<service android:name=".service.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
This service is run on when the app is live and running in the background too. But when the app is not in the background the service is not run anymore.
I have registered the service on Main Activity as follow
#Override
protected void onResume() {
super.onResume();
// register GCM registration complete receiver
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(Config.REGISTRATION_COMPLETE));
// register new push message receiver
// by doing this, the activity will be notified each time a new message arrives
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(Config.PUSH_NOTIFICATION));
// clear the notification area when the app is opened
NotificationUtils.clearNotifications(getApplicationContext());
}
#Override
protected void onPause() {
super.onPause();
}
Let me know anything wrong in my code. (Possibly not) .Why notifications are not running on background. How do I overcome this issue?
Thanks in advance.
use your server data or api like this
{
"to" : "deviceToken",
"notification" : {
"body" : "Pass body here",
"title" : "Title n",
"icon" : " icon ",
"sound" : "notification sound "
}
}
//for exa.
$fields=array('to'=>fdfdfdfdsfdsdfdfdsfdsfdfdsfd" ,'notification'=>array('title'=>'mytitle','body'=>$title,'click_action'=>'abc','icon'=>'ic_stat_final','sound'=>'default','color'=>'#00aff0'),'data'=>array('ghmid'=>$hgmid,'page'=>$page));
If your data is of type notification ie "Notification"then the onReceived method is not supposed to be get called when app is in background. Though it can be retrived when app comes to foreground. If it is of type data then it will get called.Change your type to "Data" instead of notification. Also when you change the data to type "Data" your getNotification() method may not work and app will get null pointer exception. Server data can also be of type where it has both "data" and "notification".
change json object key from notification to data
e.g
{
“to”: “device_ID/fcmID”,
“notification”: {
“body”: “great match!”,
“title”: “Portugal vs. Denmark”,
“icon”: “myicon”
}
}
change to
{
“to”: “device_ID/fcmID”,
“data”: {
“Nick”: “abc”,
“body”: “nfjwhhwruguig”,
“Room”: “cnrughwriugg”
},
}
Client app receives a data message in onMessageReceived() irrespective of the fact whether app is in foreground or background.
ref: https://blog.talentica.com/2017/01/20/firebase-cloud-messaging-in-android/
I'm adding Urban Airship push notifications to my Android app. My custom BroadcastReceiver is receiving the notifications when I send a test push. Here's the onReceive() method.
public class IntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent == null)
return;
String action = intent.getAction();
if (action.equals(PushManager.ACTION_PUSH_RECEIVED)) {
int id = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, 0); //Breakpoint here
Log.w("my app", "Received push notification. Alert: "
+ intent.getStringExtra(PushManager.EXTRA_ALERT)
+ " [NotificationID="+id+"]");
logPushExtras(intent);
} else if (action.equals(PushManager.ACTION_NOTIFICATION_OPENED)) {
//Other stuff here
}
Execution stops at the breakpoint (position shown in the comment) and the details are logged.
No notification is shown in the notification area though. In my other app with UA push notifications, I think this was all I did, but it doesn't seem to be working this time.
I suspect I've done something wrong in my manifest file or that I've forgotten to implement a class somewhere.
Any ideas?
Extra Info
Extended Application:
public class ExtendedApplication extends Application {
public void onCreate(){
AirshipConfigOptions options = AirshipConfigOptions.loadDefaultOptions(this);
UAirship.takeOff(this, options);
PushManager.enablePush();
PushPreferences prefs = PushManager.shared().getPreferences();
Log.e("my app", "My Application onCreate - App APID: " + prefs.getPushId());
PushManager.shared().setIntentReceiver(IntentReceiver.class);
}
}
Edit
I've tried adding
PushManager.shared().setNotificationBuilder(new BasicPushNotificationBuilder());
to my ExtendedApplication as suggested in this post.
I was sending a test push with a blank "Alert" field from the UA site. I tried with some text in there and now it works. I don't know why, but I don't think that's something that should fail silently.
Moral of the story: Don't leave your Alert field blank.
How to change the notification message in Android to localised it?
I just want to change the message what I get from Urban AirShip before it will be display on notification bar?
You can using this method: PushManager.shared().setNotificationBuilder(null); and send your own notification with android notification from the SDK
Yes you can modify the message once you receive the message from Urban AirShip for your internal use in the app but can't show the modified message in the notification bar.
You can check the message in your IntentReceiver
public class IntentReceiver extends BroadcastReceiver {
private static final String logTag = "Hey";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(logTag, "Received intent: " + intent.toString());
String action = intent.getAction();
if (action.equals(PushManager.ACTION_PUSH_RECEIVED)) {
int id = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, 0);
Log.v(logTag, "Received push notification. Alert: " + intent.getStringExtra(PushManager.EXTRA_ALERT)
+ ". Payload: " + intent.getStringExtra(PushManager.EXTRA_STRING_EXTRA) + ". NotificationID="+id);
//can get your message here
} else if (action.equals(PushManager.ACTION_NOTIFICATION_OPENED)) {
Log.v(logTag, "User clicked notification. Message: " + intent.getStringExtra(PushManager.EXTRA_ALERT)+ ".Payload:" + intent.getStringExtra("PushManager.EXTRA_STRING_EXTRA"));
Intent launch = new Intent(Intent.ACTION_MAIN);
UAirship.shared().getApplicationContext().startActivity(launch);
} else if (action.equals(PushManager.ACTION_REGISTRATION_FINISHED)) {
Log.i(logTag, "Registration complete. APID:" + intent.getStringExtra(PushManager.EXTRA_APID)
+ ". Valid: " + intent.getBooleanExtra(PushManager.EXTRA_REGISTRATION_VALID, false));
}
}
}
I found the solution, its kind a dirty one, but right know I need to use this.
Just after get notification from Urban AirShip I cancel all and send my own with changed message.
I read the answers and came up with a combined solution.
You want to disable UrbanAirship's default notification handler. If you do that, it won't generate and show you the notifications at all.
PushManager.shared().setNotificationBuilder(null); (Using David T's suggestion)
You want to build your own notification. This can be done inside your IntentReceiver. This link will do the trick.
Hope this helps others.
Please using that code to disable Urban Airship's notifications, override BasicPushNotificationBuilder:
BasicPushNotificationBuilder nb = new BasicPushNotificationBuilder() {
#Override
public Notification buildNotification(String alert,
Map<String, String> extras) {
return null;
}
};
// Disable notifications
PushManager.shared().setNotificationBuilder(nb);