Flutter Android receiver not working after restart - java

In flutter app the receivers for BOOT_COMPLETED and SMS_RECEIVED not work when the real device is restarted but work fine in 'android emulator' and the real device before the restart
permission for RECEIVED_SMS
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
permission for BOOT_COMPLETED
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Class for RECEIVE_SMS is SmsReceiver
Class for BOOT_COMPLETED is BootReceiver
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="ai.torquevision.encredeble"
android:installLocation="internalOnly">
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.BROADCAST_SMS"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:usesCleartextTraffic="true"
android:label="encredeble"
android:icon="#drawable/logo">
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
<activity
android:name="ai.torquevision.encredeble.MainActivity"
android:supportsPictureInPicture="true"
android:launchMode="singleTop"
android:theme="#style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="#style/NormalTheme"
/>
<!-- Displays an Android View that continues showing the launch screen
Drawable until Flutter paints its first frame, then this splash
screen fades out. A splash screen is useful to avoid any visual
gap between the end of Android's launch screen and the painting of
Flutter's first frame. -->
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="#drawable/launch_background"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<receiver android:name=".Utils.BootReceiver" >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
<receiver android:name=".Utils.SmsReceiver" android:permission="android.permission.RECEIVE_SMS" android:enabled="true" android:exported="true" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
BootReceiver.kt
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import kotlinx.coroutines.delay
import java.lang.Exception
class BootReceiver: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.d("encredeble", "encredeble context: " + context?.toString() + "intent: " + intent?.action);
Log.i("encredeble", "encredeble context: " + context?.toString() + "intent: " + intent?.action);
if(context == null || intent == null){
return;
}
try{
NotificationHandler.notify(context, "Restart action", "action: " + intent?.action);
Log.w("boot_broadcast_poc", "=======================> starting service...")
if(intent.action.equals("android.intent.action.BOOT_COMPLETED") || intent.action.equals("android.intent.action.QUICKBOOT_POWERON") || intent.action.equals("android.intent.action.LOCKED_BOOT_COMPLETED")){
NotificationHandler.notify(context, "Device restarted", "BOOT COMPLETED");
}
}
catch (err: Exception){
NotificationHandler.notify(context, "Restart error", "Error: " + err.message)
}
}
}
SmsReceiver.kt
import ai.torquevision.encredeble.MainActivity
import ai.torquevision.encredeble.R
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.provider.Telephony
import androidx.core.app.NotificationCompat
import android.telephony.SmsMessage
import android.util.Log
import androidx.core.app.NotificationManagerCompat
import io.flutter.embedding.engine.dart.DartExecutor
import io.flutter.embedding.engine.loader.FlutterLoader
import io.flutter.plugin.common.MethodChannel
import org.json.JSONArray
import org.json.JSONObject
class SmsReceiver: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.d("Sms receive", "===================================> SMS Received")
if(intent ==null) {
return
}
if(Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(intent.action)){
var msg = ""
var msgObj: JSONArray = JSONArray()
for (smsMessage in Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
val messageBody = smsMessage.messageBody
Log.d("msg body", "message body: " + messageBody)
msg = msg + messageBody
var map: JSONObject = JSONObject()
if(msgObj.length() == 0){
map.put("body", messageBody)
map.put("originatingAddress", smsMessage.originatingAddress)
map.put("displayOriginatingAddress", smsMessage.displayOriginatingAddress)
map.put("displayMessageBody", smsMessage.displayMessageBody)
msgObj.put(0, map)
} else {
val expMap = msgObj.get(msgObj.length() - 1) as JSONObject
if(expMap["originatingAddress"] == smsMessage.originatingAddress || expMap["displayOriginatingAddress"] == smsMessage.displayOriginatingAddress){
map = expMap
map.put("body", map["body"].toString() + messageBody)
map.put("displayMessageBody", map["displayMessageBody"].toString() + smsMessage.displayMessageBody)
msgObj.put(msgObj.length() - 1, map)
}else{
map.put("body", messageBody)
map.put("originatingAddress", smsMessage.originatingAddress)
map.put("displayOriginatingAddress", smsMessage.displayOriginatingAddress)
map.put("displayMessageBody", smsMessage.displayMessageBody)
msgObj.put(map)
}
}
}
if(context != null){
SmsMethodChannel.invoke(context, msgObj.toString());
}
}
}
}

Related

Clicking on push notification in android generated by AWS SDK for Unity not opening app

I am using AWS SNS SDK for Unity to receive push notifications in android and ios devices. I was able to get the notifications working in both but in android devices clicking on the notification does not open the app.
The codebase is more than 2 years old. Also, I am not familiar with java.
Based on my understanding the Utils.java file contains the implementation for notification's tap action. They have hardcorded the title to empty string so I don't get title in the notification. But I am not sure about the reason for application not opening when the notification is clicked.
Utils.java
package com.amazonaws.unity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.support.v4.app.NotificationCompat;
import com.unity3d.player.UnityPlayerProxyActivity;
public class Utils {
private static final int REQUEST_CODE = 1001;
private static final int NOTIFICATION_ID = 1;
public static void showNotification(Context context, String contentTitle,
String contentText) {
// Intent
Intent intent = new Intent(context, UnityPlayerProxyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,
REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context.getApplicationContext());
builder.setContentIntent(contentIntent);
builder.setContentText(contentText);
builder.setContentTitle(contentTitle);
builder.setWhen(System.currentTimeMillis());
builder.setAutoCancel(true);
Resources res = context.getResources();
builder.setSmallIcon(res.getIdentifier("app_icon", "drawable",
context.getPackageName()));
builder.setDefaults(Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
NotificationManager nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NOTIFICATION_ID, builder.build());
}
public static void showNotification(Context context, String contentText) {
showNotification(context, "", contentText);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amazonaws.unity"
android:installLocation="preferExternal"
android:versionCode="1"
android:versionName="1.0">
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"/>
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.amazonaws.unity.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.amazonaws.unity.permission.C2D_MESSAGE" />
<application
android:theme="#android:style/Theme.NoTitleBar"
android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:debuggable="true">
<activity android:name="com.unity3d.player.UnityPlayerNativeActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
</activity>
<receiver
android:name="com.amazonaws.unity.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.amazonaws.unity" />
</intent-filter>
</receiver>
<service android:name="com.amazonaws.unity.GCMIntentService" />
</application>
</manifest>

Push notify doesn't arrive

I build an android app composed by a web view, in parse.com i've got my device registered but when i send a push notification in "pushes sent" there is 0 pushes.
this is my Mainactivity:
import com.party.bparty.MainActivity;
import com.parse.Parse;
import com.parse.ParseInstallation;
import com.parse.ParsePush;
import com.parse.ParseQuery;
import com.parse.PushService;
public class MainActivity extends ActionBarActivity {
private WebView mWebView;
private String url = "http://www.bestparty.altervista.org";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//attivo parse per le notifiche
Parse.initialize(this,"zJxpd9798Ns6A9rzUDpe78ElRY0I99ES3LD6nDQV","kmAmA1iTbC32BTE9ERtzNOoHXbJchhIn6tyPXKMi");
PushService.setDefaultPushCallback(this, MainActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
//...
//...initializing and loading the contentview and the webview
}
//options menu omitted
}
this is my MycustomReceiver
public class MyCustomReceiver extends BroadcastReceiver {
private static final String TAG = "MyCustomReceiver";
#Override
public void onReceive(Context context, Intent intent) {
try {
if (intent == null)
Log.d(TAG, "Receiver intent null");
else {
String action = intent.getAction();
Log.d(TAG, "got action " + action );
if (action.equals("com.party.bparty.UPDATE_STATUS")) {
String channel = intent.getExtras().getString("com.parse.Channel");
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
Log.d(TAG, "got action " + action + " on channel " + channel + " with:");
Iterator itr = json.keys();
while (itr.hasNext()) {
String key = (String) itr.next();
if (key.equals("customdata")) {
Intent pupInt = new Intent(context, ShowPopUp.class);
pupInt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
context.getApplicationContext().startActivity(pupInt);
}
Log.d(TAG, "..." + key + " => " + json.getString(key));
}
}
}
}
catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
}
}
}
and this is my ShowPopup
public class ShowPopUp extends Activity implements OnClickListener {
Button ok, cancel;
boolean click = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Cupon");
setContentView(R.layout.popupdialog);
ok = (Button)findViewById(R.id.popOkB);
ok.setOnClickListener(this);
cancel = (Button)findViewById(R.id.popCancelB);
cancel.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
finish();
}
}
And this is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.party.bparty"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="com.androidhive.pushnotifications.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.androidhive.pushnotifications.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<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="com.party.bparty.ShowPopUp"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.RECEIVE_BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="com.party.bparty.MyCustomReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="com.iakremera.pushnotificationdemo.UPDATE_STATUS" />
</intent-filter>
</receiver>
</application>
</manifest>
Where's my error?
I do it, but it doesn't work anyway
" if (action.equals("com.party.bparty.UPDATE_STATUS")) {
Pick one, either com.party.bparty.UPDATE_STATUS or com.iakremera.pushnotificationdemo.UPDATE_STATUS"
but Now i have 1 "PUSH SENT" in push sent, but my device doesn't show any push
Your receiver has the wrong filter. Your receiver is declared as this:
<receiver android:name="com.party.bparty.MyCustomReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="com.iakremera.pushnotificationdemo.UPDATE_STATUS" />
</intent-filter>
</receiver>
but you are checking for a different intent action in the code:
if (action.equals("com.party.bparty.UPDATE_STATUS")) {
Pick one, either com.party.bparty.UPDATE_STATUS or com.iakremera.pushnotificationdemo.UPDATE_STATUS and also make sure that Parse knows which one it is when you perform the push itself.

I got message id from gcm. but I'm not receiving on emulator

this is the sender code which is used to send message to gcm.
//sJSONObject obj = new JSONObject();
Sender sender = new Sender(GOOGLE_SERVER_KEY);
Message message = new Message.Builder().timeToLive(120)
.delayWhileIdle(true).addData(MESSAGE_KEY, userMessage).build();
System.out.println("regId: " + regId);
Object result = sender.send(message, regId, 5);
if (StringUtils.isEmpty(((Result) result).getErrorCodeName())) {
System.out.println("success");
System.out.println(result.toString());
}
else{
System.out.println(((Result) result).getErrorCodeName());
System.out.println(result.toString());
}
Manifest file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="com.javapapers.android.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.javapapers.android.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".RegisterActivity"
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="com.javapapers.android.MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name" >
</activity>
<receiver
android:name=".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.javapapers.android" />
</intent-filter>
</receiver>
<service android:name=".GCMNotificationIntentService" />
<!-- added by shashank -->
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
CCMBroadcastreciever:
package com.javapapers.android;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("GCMBroadcastReceiver", "OnReceive method");
System.out.println( "Broadcast receiver OnReceive method");
ComponentName comp = new ComponentName(context.getPackageName(),
GCMNotificationIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
GCMNotificationIntentService:
package com.javapapers.android;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gms.gcm.GoogleCloudMessaging;
public class GCMNotificationIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GCMNotificationIntentService() {
super("GcmIntentService");
}
public static final String TAG = "GCMNotificationIntentService";
#Override
protected void onHandleIntent(Intent intent) {
Log.d("GCMNotificationIntentService", "OnHandleIntent method");
System.out.println( "GCMNotificationIntentService OnHandleIntent method");
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
sendNotification("Deleted messages on server: "
+ extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
for (int i = 0; i < 3; i++) {
Log.i(TAG,
"Working... " + (i + 1) + "/5 # "
+ SystemClock.elapsedRealtime());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Completed work # " + SystemClock.elapsedRealtime());
sendNotification("Message Received from Google GCM Server: "
+ extras.get(Config.MESSAGE_KEY));
Log.i(TAG, "Received: " + extras.toString());
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
Log.d(TAG, "Preparing to send notification...: " + msg);
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.gcm_cloud)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Log.d(TAG, "Notification sent successfully.");
}
}
Output:
success
[ messageId=0:1414564158966354%31e4cc17f9fd7ecd ]
Still i'm not getting push notification on emulator. i have tried it on device as well. same problem occurs there as well.
I had a similar problem. I solved it using the classes in This GitHub example specifically noting the following:
Three 'services' and one 'receiver' together with all the permissions added in the github example.
AndroidManifest.xml
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE" />
<permission
android:name="<yourpackagename>.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="<yourpackagename>.permission.C2D_MESSAGE" />
<service
android:name=".utility.MyGcmRegistrationIntentService"
android:exported="false" >
</service>
<service
android:name=".utility.MyGcmListenerService"
android:exported="false" >
<intent-filter>
actionandroid:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter</service>
<service
android:name=".utility.MyInstanceIdListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
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.yourpackagename>" />
</intent-filter>
</receiver>
The Receiver is implemented in the
ActivityMain.java:
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
boolean sentToken = sharedPreferences.getBoolean(SENT_TOKEN_TO_SERVER, false);
if (sentToken) {
} else {
}
}
};
#Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter("registrationComplete"));
}
#Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
super.onPause();
}
You can basically use the other files just like they are:
MyGcmListenerService.java - The one where you can customize your notification icon and text.
MyInstanceIDListenerService.java
QuickstartPreferences.java
RegistrationIntentService.java

Broadcast receiver not getting called on SMS_RECEIVED [duplicate]

What I've done
Hello Guys, I'm creating at the moment a SMS Broadcast Receiver, i just builded one up with this tutorial: Broadcasttutorial. After I did the code, I updated my Manifest. After that I sent sms from my other Phone to my Phone, but It didn't work. I didn't get any output.
Question
What do I need to change, that I can receive those SMS. Please gimme a detailed anwser that I can learn it, a good tutorial would also be great!
Code
SMSBroadcastReceiver (is in package .services)
package de.retowaelchli.filterit.services;
import de.retowaelchli.filterit.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class SmileySmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Log.d("SmileySmsReceiver", "Yes it calls the onReceive");
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
This is my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.retowaelchli.filterit"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<!-- User Permission -->
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<application android:icon="#drawable/icon"
android:label="#string/app_name"
android:debuggable="true"
android:screenOrientation="sensor"
android:theme="#style/FilterIt.Theme">
<activity android:name=".SplashScreenActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Receiver -->
<receiver android:name="de.retowaelchli.filterit.services.SmileySmsReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<!-- Startseite -->
<activity android:name=".StartseiteActivity"></activity>
<!-- Von Startseite ausgehende Activitys -->
<activity android:name=".SmileyActivity"></activity>
<activity android:name=".ADeleteActivity"></activity>
<activity android:name=".StatsActivity"></activity>
<activity android:name=".HelpMenuActivity"></activity>
<!-- Von Stats ausgehende Activitys -->
<activity android:name=".stats.ADFilterStats"></activity>
<activity android:name=".stats.SFilterStats"></activity>
<activity android:name=".stats.CreatedADFilters"></activity>
<activity android:name=".stats.CreatedSFilters"></activity>
<!-- Von ADeleteActivity ausgehende Activitys -->
<activity android:name=".ADFilterConfigActivity"></activity>
<!-- Von SmileyActivity ausgehende Activitys -->
<activity android:name=".SFilterConfigActivity"></activity>
</application>
</manifest>
Put <uses-permission android:name="android.permission.RECEIVE_SMS" /> outside of the <application> tag:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.retowaelchli.filterit"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application android:icon="#drawable/icon"
android:label="#string/app_name"
android:debuggable="true"
android:screenOrientation="sensor"
android:theme="#style/FilterIt.Theme">
<!-- Receiver -->
<receiver android:name="de.retowaelchli.filterit.services.SmileySMSBroadcastReceiver">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
…
…
</application>
</manifest>
UPDATE
Turned out that #safari uses "Handcent SMS" application on his phone which intercepts incoming SMS (this is possible because SMS_RECEIVED is an ordered broadcast and can be canceled by high priority broadcast receivers, refer to this thread for details).
To bypass this issue one would need to install broadcast receiver with higher priority than "Handcent SMS". #safari used the highest priority allowed for applications in Android: 999, and it worked for him.
To specify priority of broadcast receiver add android:priority attribute to corresponding <intent-filter> item:
<receiver android:name="YourSmsBroadcastReceiver">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
for (int i=0; i<pdus.length; i++)
instead of msgs.length, pdus contains the real sms messages.
gauglerb pointed me in the right direction here with his comment for the accepted answer and I think I should share my findings.
Handcent has indeed been a bad boy and are not letting any other apps receive messages when it is installed.
Fortunately there is an easy solution if you don't want to uninstall Handcent:
In the Application settings of Handcent there is an option to make Handcent the default Messaging application. If this is disabled, messages can come through to other receivers.
Give permission in MainActivity in onCreate method. It will work.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.READ_SMS, Manifest.permission.SEND_SMS, Manifest.permission.RECEIVE_SMS}, 10);
}

Writing a SmsReceiver that doesn't let other applications to receive sms

I'm writing an app with a SmsReceiver. I want this class to receive message texts that contains a special word, like "SPY", and don't let other recievers, receive such messages. how can I do it?
here is the receiver class
package com.newidea.repairmycontacts;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import android.telephony.SmsMessage;
public class SmsReceiver extends BroadcastReceiver {
private final static String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private final String TAG = "SmsReceiver";
#Override
public void onReceive(Context context, Intent intent) {
//onReceiveWithPrivilege(context, intent, false);
if(!intent.getAction().equals(SMS_RECEIVED))
return;
Bundle bundle = intent.getExtras();
if(bundle==null)
return;
Object[] pdus=(Object[]) bundle.get("pdus");
String Body="";
for(int i=0;i<pdus.length;i++)
Body += SmsMessage.createFromPdu((byte[]) pdus[i]).getMessageBody().toString()+"\n";
String Address=SmsMessage.createFromPdu((byte[]) pdus[0]).getOriginatingAddress();
Toast.makeText(context, "Message from "+Address+" : "+Body, Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.newidea.repairmycontacts"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" >
<activity
android:name="com.newidea.repairmycontacts.SMSList"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".SmsReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
You have to set a priority on your intent-filter tag for .SmsReceiver like this:
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Be aware that this will work with stock messaging app, but some other sms apps use other priorities and may get on top of yours...
After if you want to prevent other apps from receiving the broadcast, you can use:
BroadcastReceiver.abortBroadcast()
Keep in mind that this is fragile specially with other sms apps.

Categories