GCM push notification not received (gone through all) - android

Before you point this as a duplicate, I know this has been asked many a times but it seems after the launch of new GCM (that which requires one to include play services project as library) things have changed on how to correctly implement gcm in our code.
The breakpoint in my GCMIntentService.java and GcmBroadcastReceiver.java gets hit on register since I have set <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> in the manifest but the breakpoints don't get hit when i send a push from the server. Here's all which you would ever need:
Relevant AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aceast.waveindia"
android:versionCode="1"
android:versionName="waveindia1" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="11" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission
android:name="com.aceast.waveindia.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<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.aceast.waveindia" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
GCMBroadcastReceiver.java
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
//breakpoint gets hit on register here...
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
Part of GCMIntentService which breakpoint hits on register
#Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
.NET code which triggers push
public string SendNotification()
{
var value = Message;
WebRequest tRequest;
tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
tRequest.Method = "post";
tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
// tRequest.ContentType = " application/json;charset=UTF-8";
tRequest.Headers.Add(string.Format("Authorization: key={0}", GOOGLE_APPID));
tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + DeviceID + "";
Console.WriteLine(postData);
Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
tRequest.ContentLength = byteArray.Length;
Stream dataStream = tRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse tResponse = tRequest.GetResponse();
dataStream = tResponse.GetResponseStream();
StreamReader tReader = new StreamReader(dataStream);
String sResponseFromServer = tReader.ReadToEnd();
tReader.Close();
dataStream.Close();
tResponse.Close();
return sResponseFromServer;
}
Local firewall has 5228-5230 ports unblocked
Now can you point the error?

I see a problem in your manifest :
<uses-permission
android:name="com.aceast.waveindia.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
Should be :
<permission android:name="com.aceast.waveindia.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.aceast.waveindia.permission.C2D_MESSAGE" />

Related

Unable to find SMS permission in android studio

I am unable to find android.provider.Telephony action in android studio for creating an app that can receive SMS. Almost every article including android developer says that I have to include a intent filter action android.provider.Telephony.SMS_RECEIVE in manifest file. But I figured out that this action is no more supported by android studio. Please help me
You need to give the permission to your manifest file,
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
Then you should include the intent-filter to your manifest,You should have taken a class that extends BroadcastReceiver write the name of that class as receiver name.In my case it is SMSReceivcer.
<receiver android:name=".SMSReceivcer"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
pls try this
// Add this in manifest
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<receiver android:name=".SMSReciver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
//Create new class
public class SMSReciver extends BroadcastReceiver
{
private Context mContext;
#Override
public void onReceive(Context context, Intent intent)
{
mContext = context;
Bundle myBundle = intent.getExtras();
SmsMessage[] messages = null;
String strMessage = "";
String lMessageBody = "", lMessageFrom = "";
if (myBundle != null)
{
Object[] pdus = (Object[]) myBundle.get("pdus");
messages = new SmsMessage[pdus.length];
for (int i = 0; i < messages.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
lMessageFrom = messages[i].getOriginatingAddress();
lMessageBody = messages[i].getMessageBody();
}
}
}
}

how to get receiver phone number from sms application? [duplicate]

Is it possible in android to filter the SMS from a specific number in android.And save it in a specific database.and the SMS should not be visible in the inbox...
You have yo do this in 3 step:
read SMS from Specific number you have to look in to this link then after
you have to create Database for store all info about SMS(like id, body, number, time) for that check this link
you have to Delete SMS from INBOX check this link for delete SMS.
add below permission for do all step:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
ya is it possible.
in mainfest file add following code:
<receiver android:name="com.example.Sms_BReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
after that create a class use following code
public class Sms_BReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] messageString = null;
Object[] pdus = (Object[]) bundle.get("pdus");
messageString = new SmsMessage[pdus.length];
for (i = 0; i < messageString.length; i++) {
messageString[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
Phone_no = messageString[0].getOriginatingAddress();
Message_body = messageString[0].getMessageBody();
Time = messageString[0].getTimestampMillis();
CharSequence text = str;
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
}
if (Phone_no.contains(here check no to filter) {
here delete message form inbox
and save in our own db
} else {
}
}
i hope this code will use full for u

Broadcast Receiver Long Binary SMS

I have a problem when receiving a long binary SMS. The client is supposed to receive multipart binary SMS. Upon searching stackoverflow, people have mentioned that onReceive() shall be fired only once. From there you can concatenate the multiple SMS.
I am not sure if that's the same when sending binary SMS. I receive 2 SMS, and onReceive is fired twice. The SMS are separate from each other making it impossible to concatenate.
AndroidManifest:
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.INTERNET" />
<receiver
android:name="com.myapp.smsnotification.SMSReceiver"
android:enabled="true" >
<intent-filter android:priority="100" >
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data
android:host="localhost"
android:port="9515"
android:scheme="sms" />
</intent-filter>
</receiver>
SMSReceiver.java
in my onReceive()
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
try {
if (messages.length > -1) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
for (int i = 0; i < messages.length; i++) {
SmsMessage message = messages[i];
os.write(message.getUserData());
}
byte[] stream = os.toByteArray();
// do something with stream
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
Found out that it was not an issue with the client side but in the server side. They didn't include in the User Data Header the part to indicate that the message is a concatenated SMS.
If the protocol is followed, Android will be able to detect it and handle them accordingly, waiting for all SMS to arrive and fire the SMS broadcast receiver once containing all received SMS.

can't receive messages on device using GCM

i've followed google instruction on initiating a gcm server and client.
my problem is though i get a successful gcm from server :
MulticastResult(multicast_id=8287827393174436535,total=1,success=1,failure=0,canonical_ids=0,results:
[[ messageId=0:1366468335181772%8e1522ac00000031 ]]
i'm not able to see any movement of receiving a message on the client - onMessage() isn't called.
client id as well as server id are correct as far as i can tell.
i'd appreciate any help... :)
the server side code :
try {
Sender sender = new Sender(API_KEY);
Message message = new Message.Builder().collapseKey("1")
.timeToLive(3)
.delayWhileIdle(true)
.addData("message",
"this text will be seen in notification bar!!").build();
ArrayList<String> devices = new ArrayList<String>();
devices.add(DEVICE_ID1);
//devices.add(DEVICE_ID2);
MulticastResult result = sender.send(message, devices, 2);
//Result result = sender.send(message, DEVICE_ID1, 2);
System.out.println(result.toString());
if (result.getResults() != null) {
int canonicalRegId = result.getCanonicalIds();
if (canonicalRegId != 0) {
}
} else {
int error = result.getFailure();
System.out.println(error);
}
} catch (Exception e) {
// TODO: handle exception
}
the client code :
registering :
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, SENDER_ID); // Note: get the sender id from configuration.
String regIdString = GCMRegistrar.getRegistrationId(this);
Log.v(TAG, "RegisterId, regId: " + regIdString);
} else {
Log.v(TAG, "Already registered, regId: " + regId);
}
and gcmIntentServiceCreated (partly) :
public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService(){
super(SENDER_ID);
}
#Override
protected void onMessage(Context context, Intent intent) {
Log.i("Registration", "Got a message!");
Log.i("Registration", context.toString() + " " + intent.toString());
}
...
}
client's manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission android:protectionLevel="signature" android:name="com.example.myapp.permission.C2D_MESSAGE"></permission>
<uses-permission android:name="com.example.myapp.permission.C2D_MESSAGE"/>
<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.myapp.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>
<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.example.myapp" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
</manifest>
To maximise your chances of receiving the message, I would not set the time_to_live at a mere 3 seconds. I'd omit this parameter altogether so that it takes the default value of 4 weeks. I would also set delay_while to false, so that the phone gets the message even whilst idle.
Well your message is surely getting sent to your device since gcm sends a success status of 1. Try to check the GCMReceiver code of your app.
Change the following code in your onMessage method:
Log.i("Registration", "Got a message!");
Log.i("Registration", context.toString() + " " + intent.toString());
to this:
String message=intent.getStringExtra("message");
Log.w("The message received is", message);
hope this help

listening incoming sms on android

the following program is my sample program for Listening incoming sms.It is created .apk file
with out error but it does not display the message please help me.the toast does not display
any message if the emulator receive the message.
My scenario is receive the sms ansd display the alert dialog box to user.that sms contanins
email address depending on that address my app search the phone contacts and send the contact
number of the emailId's person as reply message
public void onReceive(Context context,Intent intent)
{
Bundle extras=intent.getExtras();
String messages="";
if(extras!=null)
{
Object[] smsExtra=(Object[]) extras.get("pdus");
for(int i=0;i<smsExtra.length;i++)
{
SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
String body = sms.getMessageBody().toString();
String address = sms.getOriginatingAddress();
messages += "SMS from " + address + " :\n";
messages += body + "\n";
}
Toast.makeText(context, messages, Toast.LENGTH_SHORT).show(); // not display
}
}//onReceive
my manifastfile
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="e.x.x"
android:versionCode="1"
android:versionName="0.1" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<receiver android:name=".ex2" android:exported="true" >
<intent-filter android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</manifest>
I'm not sure about manifest. Try like in this example Android – Listen For Incoming SMS Messages
I think android:priority="999" and android:exported="true" is a root of problem

Categories

Resources