C2DMReg - handleRequest caught java.net.UnknownHostException: android.clients.google.com - android

My question is related to [C2DMReg] handleRequest caught java.net.UnknownHostException: android.clients.google.com I found some question in StackOverflow with the same title that has been marked as duplicate. But the duplicate that is mentioned has nothing to do with the matter at concern here. This is related to the new GCM - not C2DM.
My code sources are from the GCM tutorial in Android API Guide
My function for registering the device with GCM is as follows:
public void register_device(Context c){
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(c, 0, new Intent(), 0));
registrationIntent.putExtra("sender", c.getResources().getString(R.string.SERVER_API_KEY));
Log.i("GCM", "Starting service for registration Intent");
}
My BroadCastReceiver class is as follows:
public class MyBroadcastReceiver extends BroadcastReceiver {
public final void onReceive(Context context, Intent intent) {
Log.i("GCM", "Received broadcast from Intent: " + intent);
MyIntentService.runIntentInService(context, intent);
setResult(MainActivity.RESULT_OK, null, null);
}
}
My Manifest file contains the permissions for MyBroadcastReceiver as follows
<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.mangoesmobile.praxis.pusher.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.mangoesmobile.praxis.pusher.permission.C2D_MESSAGE" />
<application>
<receiver
android:name=".MyBroadcastReceiver"
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.mangoesmobile.praxis.pusher" />
</intent-filter>
</receiver>
</application>
The logs show my custom log from the register_device function() Starting service for registration Intent
and then shows [C2DMReg] handleRequest caught java.net.UnknownHostException: android.clients.google.com
with tag C2DMRegistrar
It never shows my custom log Received broadcast from Intent from MyBroadcastReceiver. So it never reaches MyBroadcastReceiver i believe. I am not able to figure why would this happen.
I am not using an emulator. I am using my own device and it is registered with a google account.

Related

Android Push Notifications with Parse on Back4App not being received

I've configured Push notifications in Android for an app following all the instructions in:
https://github.com/ParsePlatform/Parse-Server/wiki/Push-Configuring-Clients
https://parseplatform.github.io/docs/android/guide/#push-notifications
But no matter what I do, the mobile phone I'm using can't seem to get any push notifications at all.
The same app is configured to receive the notifications on iOS and works perfectly.
These are the relevant bits in the Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxy.xxy">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<permission
android:name="com.xxy.xxy.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.xxy.xxy.permission.C2D_MESSAGE" />
<application
android:name="com.xxy.xxy.MiXXYApplication"
android:allowBackup="true"
android:icon="#drawable/logoapp"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<service android:name="com.parse.PushService" />
<receiver
android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.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.xxy.xxy" />
</intent-filter>
</receiver>
<meta-data
android:name="com.parse.push.gcm_sender_id"
android:value="id:123456789012" />
</application>
This is the Application class (with the AppID and Client Key replaced with the current value given by Back4App):
public class MiXXYApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
Parse.setLogLevel(Parse.LOG_LEVEL_VERBOSE);
Parse.initialize(new Parse.Configuration.Builder(getApplicationContext())
.applicationId("back4App_AppID")
.clientKey("back4App_ClientKey")
.server("https://parseapi.back4app.com/")
.build()
);
}
}
These are the methods I call to register the installation (I can't do it on the Application class because I need the userId, after login)
public void enablePush(final String userId) {
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
installation.put("GCMSenderId", "123456789012");
installation.put("userId", userId);
installation.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.i("push", "ok");
subscribe("");
subscribe(userId);
} else {
Log.i("push", "nok");
e.printStackTrace();
}
}
});
}
public void subscribe(String channel){
ParsePush.subscribeInBackground(channel, new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.i("Push", "subscribed");
} else {
Log.i("push", "nok");
e.printStackTrace();
}
}
});
}
This is the installation registry on the Parse DB:
ObjectID: l1tkO3YM2r
GCMSenderID: 123456789012
deviceToken: APA91bHeKnj...
localeIdentifier: en-US
badge: (undefined)
parseVersion: 1.13.1
ACL: Public Read + Write
appIdentifier: com.xxy.xxy
appName: appName
deviceType: android
channels: ["","CyTPw5xc4r"]
pushType: gcm
installationId: 8cf9c606-5a0e...
userId: CyTPw5xc4r
In the google developer console, the project number is 123456789012, I've created an API key and added the API key + the project number in the Back4App dashboard's Android Push Notification Settings.
The logcat in verbose mode is showing the following:
09-21 09:29:19.863 10721-10721/com.xxy.xxy V/com.parse.ManifestInfo: Using gcm for push.
09-21 09:29:19.884 10721-10885/com.xxy.xxy V/com.parse.CachedCurrentInstallationController: Successfully deserialized Installation object
09-21 09:29:19.899 10721-10886/com.xxy.xxy V/com.parse.GcmRegistrar: Sending GCM registration intent
09-21 09:29:29.782 10721-11340/com.xxy.xxy V/com.parse.GcmRegistrar: Received deviceToken <APA91bHeKnj...> from GCM.
For what I can tell, everything's good to go, but I'm not getting any notification in my phone.
Is there anything wrong with my setup?
Thanks!
Solved it!
The thing is that, instead of getting the Api key the way all the guides say, you should get it from https://developers.google.com/mobile/add.
It generated another API key and activated the service, and then, it worked.

My app does not receive Parse push notifications

I have setup my app for Parse notifications and strictly followed the instructions, but somehow I don't receive any notification. What is the problem here?
Update: I just tried sending a test push notification on the Parse.com website. That should work without subscribing to a channel (see comment)
My AndroidManifest.xml (only the relevant parts):
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:protectionLevel="signature"
android:name="co.bla.bla.permission.C2D_MESSAGE" />
<uses-permission android:name="co.bla.bla.permission.C2D_MESSAGE" />
<!-- Push notification setup -->
<service android:name="com.parse.PushService" />
<receiver android:name="co.bla.bla.ParsePushReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.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="co.bla.bla" />
</intent-filter>
</receiver>
</application>
And I have extended ParsePushBroadcastReceiver:
public class ParsePushReceiver extends com.parse.ParsePushBroadcastReceiver{
#Override
protected void onPushReceive(Context context, Intent intent ) {
ParseAnalytics.trackAppOpenedInBackground(intent);
String s = intent.getStringExtra("alert");
Log.d("Push received", s);
// do your stuff here
if(SugarSnapApplication.INACTIVE)
super.onPushReceive(context, intent);
}
#Override
protected void onPushOpen(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent i = new Intent(context, SugarActivity.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
#Override
protected Notification getNotification(Context context, Intent intent) {
// TODO Auto-generated method stub
return super.getNotification(context, intent);
}
#Override
protected void onPushDismiss(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onPushDismiss(context, intent);
}
}
I finally found out what the problem was. I have renamed the whole package name of my application a couple of days ago and apparently the change was not made everywhere and the applicationId in my build.gradle file was still the old one. So naturally the package name in my AndroidManifest.xml was not the same as the one saved in my Installation class.

How to catch on Wifi Change?

I'm developing an app without GUI. It should start it's service when Wifi button is changed(ON or OFF).
This is my manifest file:
<receiver android:name="com.updater.wifitester.NetWatcher">
<intent-filter
android:priority="100">
<action
android:name="android.net.wifi.WIFI_STATE_CHANGED"
android:enabled="true"
/>
</intent-filter>
</receiver>
Here is the permissions that I gave:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
And I extended Broadcast receiver:
public class NetWatcher extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
//check intents and service to know if all is ok then set bWifiStateOk accordingly
Toast.makeText(context, "Wifi on", Toast.LENGTH_LONG).show();
Log.i("wifi_state_changed","wifi changed...");
} else {
return ; // do nothing ... we're not in good intent context doh !
}
}
}
But there is nothing in the log cat and no Toasts shown. What Am I missing?
Please help me guys...
Try this
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>

BroadCast Receiver is not Working

Hi i asked a question before related to Broadcast here
they suggest me to add broadcast receiver class in Manifest file and then register its instances in Activity's onCreate method
so here is code of my manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.riteshmapsapplicationdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:icon="#drawable/app_icon"
android:label="Tring On Train"
android:theme="#style/AppTheme" >
<uses-library android:required="true"
android:name="com.google.android.maps"/>
<activity
android:name=".MainActivity"
android:label="Tring On Train" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AlarmActivity"
android:label="Alarm" />
<activity
android:name=".Address_List_Activity"
android:label="Saved Locations" />
<receiver android:name=".ProximityAlertReceiver" >
</receiver>
</application>
</manifest>
and here is code segment of onCreate's method where i am registering instances of BroadCast Receiver class.
String s=System.currentTimeMillis()+"";
PendingIntent proximityIntent = PendingIntent.getBroadcast(getApplicationContext(), i, intent, PendingIntent.FLAG_UPDATE_CURRENT);
locationManager.addProximityAlert(
latitude, // the latitude of the central point of the alert region
longitude, // the longitude of the central point of the alert region
1000, // the radius of the central point of the alert region, in meters
-1, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration
proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected
);
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT+s);
registerReceiver(new ProximityAlertReceiver(), filter);
and code of My ProximityAlert class is
public class ProximityAlertReceiver extends BroadcastReceiver {
private static final int NOTIFICATION_ID = 1000;
public static final String PREFS_NAME = "MyPrefsFile";
#Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
SharedPreferences shared=context.getSharedPreferences(MainActivity.PREFS_NAME,0);
Boolean entering = intent.getBooleanExtra(key, false);
Double longitude=intent.getDoubleExtra("longitude", 0.0);
Double latitude=intent.getDoubleExtra("latitude",0.0);
Intent in = new Intent(context,AlarmActivity.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in.putExtra("longitude", longitude);
in.putExtra("latitude", latitude);
Log.i("cont1", shared.getInt("count", 0)+"");
in.putExtra("count",shared.getInt("count", 0));
context.startActivity(in);
}
}
now last time Broadcast receiver working when activity is in foreground but this time Broadcast receiver is not working in any situation can any one please notify me where i am wrong??
Your not defined what type of broadcast receiver you want to receive..try to add intent-filter in the manifest receiver tag. The the android will send you.
You want to receive broadcasts when not running Activity in foreground. For that you need to add valid intent-filter to your receiver in AndroidManifest.xml
For example:
<receiver android:name=".ProximityAlertReceiver" >
<intent-filter>
<action android:name="com.your.app.example_filter_1" />
<action android:name="com.your.app.example_filter_2" />
<!-- etc -->
</intent-filter>
</receiver>

Error:- getting error while getting registration id for c2dm

I have Written the application for getting registration id for c2dm. but i am getting Exception as unable to start activity component info at line startService(intent);
My main class is
public class IdTestActivity extends Activity {
static TextView mytext = null;
Context context = null;
Intent intent = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
intent.putExtra("app",PendingIntent.getBroadcast(this, 0, new Intent(), 0));
intent.putExtra("sender", "dvimayandroid#gmail.com");
startService(intent);
}
}
My Receiver class is
public class c2dmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.w("C2DM", "Registration Receiver called");
if ("com.google.android.c2dm.intent.REGISTRATION".equals(action)) {
Log.w("C2DM", "Received registration ID");
final String registrationId = intent
.getStringExtra("sender");
String error = intent.getStringExtra("error");
Log.d("C2DM", "dmControl: registrationId = " + registrationId
+ ", error = " + error);
// TODO Send this to my application server
}
}
}
and my manifest file is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.IdTest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application>
<permission
android:name="com.IdTest.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.IdTest.permission.C2D_MESSAGE" />
<activity
android:name=".IdTestActivity"
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=".c2dmReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter >
<action android:name="com.google.android.c2dm.intent.RECEIVE" >
</action>
<category android:name="com.IdTest" />
</intent-filter>
</receiver>
</application>
</manifest>
It looks like you need to add a permission to your manifest. Check what's needed here, and make sure your app includes everything:
http://code.google.com/android/c2dm/#manifest
you should add the permission to reveive messages from C2DM Servers like this in your manifest :
<!-- This app has permission to register and receive message -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
i think you should add the registration intent filter to your service by this you could get the registration id
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="your pacakge" />
</intent-filter>
also you to get the coming registration id you use
intent.getStringExtra("registration_id");
hope that could help you

Categories

Resources