How to get push notification with urban airship? - android

I am trying to develop the push notification with Urban Airship. I am successfully running the app, but my device is not registering in my Account. ANd I am getting these errors also in logcate.
01-24 19:20:47.480: W/GCMNotification - UALib(30560): Activity com.example.gcmnotification.MainActivity#416c6fb8 was not manually added during onStart(). Call UAirship.shared().getAnalytics().activityStarted in every activity's onStart() method.
01-24 19:21:14.400: E/GCMNotification - UALib(31088): AndroidManifest.xml missing required service: com.urbanairship.analytics.EventService
01-24 19:21:14.410: E/GCMNotification - UALib(31088): AndroidManifest.xml's com.urbanairship.push.GCMPushReceiver declaration missing required com.google.android.c2dm.intent.RECEIVE filter with category=com.example.gcmnotification
01-24 19:21:14.410: E/GCMNotification - UALib(31088): AndroidManifest.xml's com.urbanairship.push.GCMPushReceiver declaration missing required com.google.android.c2dm.intent.REGISTRATION filter with category=com.example.gcmnotification
code is here.
public class MyApplication extends Application{
#Override
public void onCreate() {
AirshipConfigOptions options = AirshipConfigOptions.loadDefaultOptions(this);
options.developmentAppKey = "pahvQlDxxx";
options.developmentAppSecret = "bOltfxxx";
options.productionAppKey = "AIzaSyCS_QxF-AtTglLf5BWxxx";
options.inProduction = false;
//options.iapEnabled = false;
UAirship.takeOff(this, options);
PushManager.enablePush();
}
}

The LogCat tells you exactly what is missing in your AndroidManifest.xml.
Your broadcast receiver should be declared as follows :
<receiver android:name="com.urbanairship.push.GCMPushReceiver"
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.gcmnotification" />
</intent-filter>
</receiver>
And you should declare com.urbanairship.analytics.EventService :
<service android:name="com.urbanairship.analytics.EventService" />

Related

FCM not receiving notification messages

I have my manifest in check, my services in check, my dependencies in check, class variables in check and still no notifications on my device. I'm using my computer to send firebase cloud messages and I should be receiving them on my android phone. However,my android phone is not picking up the notifications. And yes, my android device is in the background as I send the message. Anyone know what my problem is?
Here's the source code:
My Service
public class FirebaseIDMessage extends FirebaseMessagingService {
private static final String TAG = "FirebaseIDMessage";
#Override
public void onNewToken(String s) {
super.onNewToken(s);
String token = s;
Log.d(TAG, "Registered token: = " + token);
sendRegistrationToServer(token);
}
private void sendRegistrationToServer(String token){
}
}
My Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.messaging">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<!-- Services -->
<service android:name=".FirebaseIDMessage"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<activity android:name=".MessagingActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
My Activity
public class MessagingActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messaging);
}
}
In your manifest file, you are missing INSTANCE_ID_EVENT
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
This is the intent-filter for the Class extended with FirebaseInstanceIdService
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService
{
public static final String REGISTRATION_TOKEN = "REG_TOKEN";
#Override
public void onTokenRefresh()
{
String token = FirebaseInstanceId.getInstance().getToken();
Log.e(REGISTRATION_TOKEN,token);
}
}
So, finally the manifest service will be like
<service android:name=".Utils.PushNotification.MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
On the log you would have got registered Token. Your all set only if you receive that token.
Goto Firebase console : https://console.firebase.google.com
Choose your project -> From the left side menu -> Choose Cloud Messaging -> New Message
Type your message and then in the TARGET choose Single Device and then copy paste your received token and check on your device for this notification by this way we can confirm you have set all notification related code correctly. If you still face any problem please let know so that I can share some samples.

Firebase onMessageReceived() not called via pyfcm

I am trying to pass "badge" numbers via FCM.
I know that onMessageReceived() only get called when :
[payload contains only "data", no message]
I am using "com.google.firebase:firebase-messaging:11.0.2"
and pyfcm on server side to send my notification by this.
result = push_service.notify_multiple_devices(
registration_ids=ids, data_message={'badge': '5'}
)
>> all success.
My Service :
public class MyFCMService extends FirebaseMessagingService {
private static final String TAG = "MyFCMService";
public MyFCMService() {
}
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, remoteMessage.getData().toString());
super.onMessageReceived(remoteMessage);
}
}
My Service in manifest.xml:
<service android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name=".MyFCMService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
I have read this but I didn't close the app (just press HOME)
but still, logcat(verbose) shows nothing.
Is there any possible reasons?
I found the reason while copying my code in Manifest file...
it should be
<application>
...
<service
android:name=".libs.fcm.MyFCMService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</application>
NOT
<service
android:name=".libs.fcm.MyFCMService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<application>
...
</application>
result:
D/MyFCMService: {badge=5}
and badge number can be update by ShortcutBadger through notification
with a datapayload contains badge.
pyfcm:
result = push_service.notify_multiple_devices(registration_ids=registration_ids, data_message=data_message, click_action=click_action, message_title=message_title,message_body=message_body, message_icon=message_icon)
Android Service
#Override
public void handleIntent(Intent intent) {
if(intent.hasExtra("badge"))
}

Google Cloud Messaging returns Failure: HTTP Status Code: 401

I am using online tool to send Notification to my Device
http://gcm-alert.appspot.com/
http://techzog.com/development/gcm-notification-test-tool-android/
They both returns,
Failure: HTTP Status Code: 401 -Unauthorized
I have followed following steps to implement Google Clound Messaging API
Create Configuration File added it in my Project->app Folder
Created Async Task In Main Activity and write Following Code.
SharedPreferences getPreference= getSharedPreferences("GCMToken", Context.MODE_PRIVATE);
String tokenID= getPreference.getString("tokenID","");
if(tokenID.isEmpty()) {
InstanceID gcmTokenistanceID = InstanceID.getInstance(getApplicationContext());
token = gcmTokenistanceID.getToken(getString(R.string.project_id),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
GCMServiceURL= "MyWebServerURL.com/="+token;
Log.i("TOKEN From IF Condition", token);
saveTokenID(token);
Then I created NotificationService and extend this class with GcmListenerService
public class GCMNotificationService extends GcmListenerService {
#Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("Test Message from Server");
NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText(message);
notificationManager.notify(1, mBuilder.build());
}
Manifest File
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.app.myapp.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.app.myapp.permission.C2D_MESSAGE" />
<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.app.myapp" />
</intent-filter>
</receiver>
<service
android:name="com.app.myapp.GCMNotificationService" android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
Kindly guide me what i am doing wrong here
Add this permission
<uses-permission android:name="android.permission.INTERNET" />

Service not Starting when i destroy and start via Receiver

When i start service via Broadcast Receiver its showing me the logcat
01-29 12:20:33.245: W/ContextImpl(4721): Implicit intents with startService are not safe: Intent { act=actions.com.sound_profile_change.MyService } android.content.ContextWrapper.startService:494 android.content.ContextWrapper.startService:494 actions.com.sound_profile_change.MyReceiver.onReceive:30
01-29 12:20:33.245: W/ActivityManager(511): Unable to start service Intent { act=actions.com.sound_profile_change.MyService } U=0: not found
In Service Side:
#Override
public void onDestroy() {
super.onDestroy();
timer.cancel();
timerTask.cancel();
Intent intent = new Intent("This.is.Receiver");
sendBroadcast(intent);
}
In Receiver Side:
#Override
public void onReceive(Context context, Intent intent) {
if(context!=null){
System.out.println("Am in Receiver ");
System.out.println("Not Null :>>"+context);
Intent services = new Intent();
services.setAction(
"actions.com.sound_profile_change.MyService");
context.startService(services);
// context.startService(new Intent(context, MyService.class));
}else{
System.out.println("Its Null");
}
}
In Manifest File:
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<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>
<service
android:name=".MyService"
android:enabled="true" />
<receiver android:name=".MyReceiver"
android:enabled="true"
android:exported="true"
>
<intent-filter>
<action android:name="This.is.Receiver"/>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
</application>
Help Me...
"Implicit intent" warning that your are getting is because Using Implicit Intents you have not mentioned a specific component (for example: name of service class).
Implicit Intents does have not specified a component; instead, they must include enough information for the system to determine which of the available components is best to run for that intent.
So, it means the component will now be chosen by Android intent-filter mechanism, by given Intent-Action.
You may try following in code while starting a service:
Intent services = new Intent(context,MyService.class);//Explicit mention of service class name while creating intent
services.setAction("actions.com.sound_profile_change.MyService");
context.startService(services);
Above code is now using "explicit intent". Explicit Intents have specified a component which provides the exact class to be run.

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

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.

Categories

Resources