NativeScript / sample-android-background-services

Using Android Background Services in NativeScript
63 stars 10 forks source link

Cannot implement broadcast receiver. Need help. #15

Closed priyangamani closed 6 years ago

priyangamani commented 7 years ago

Creating a class in JS:-

android.content.BroadcastReceiver.extend("com.tns.broadcastreceivers.NotificationEventReceiver", {
    onReceive: function (context, intent) {
console.log("enter");
        var action = intent.getAction();
           var messages=  intent.getSerializableExtra("pdus");
         var format=intent.getStringExtra("format");
        console.log("smsmanager",messages);
        console.log("smsmanagerformat",format);
    }
})
AndroidManifest file
   <receiver android:name="com.tns.broadcastreceivers.NotificationEventReceiver">   
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
petekanev commented 7 years ago

Hey @priyangamani it doesn't become clear what isn't working for you, nor whether you've registered the broadcast receiver. Please take the time to explain detail, in a structured manner, what you are trying to do.

Thank you.

priyangamani commented 7 years ago

@Pip3r4o Listen For Incoming SMS Messages by using Broadcastreceiver. Why Broadcastreceiver's onreceive() method not invoked? I'm trying to implement this code and it's not working. When I receive a message, nothing logs to the console. Is there something I'm missing?

petekanev commented 7 years ago

@priyangamani please answer the following questions:

One last thing - please use the NativeScript forums for further questions when you are unsure about a certain implementation. We try to keep the issue section strictly for addressing problems.

priyangamani commented 7 years ago

1.How have you attempted to trigger the broadcast receiver? Just i tried to extend the Broadcastreceiver class and call the onreceiver function. I mentioned Receiver tag in manifest.. //Below file i mentioned in javascript android.content.BroadcastReceiver.extend("com.tns.broadcastreceivers.NotificationEventReceiver", { onReceive: function (context, intent) { var action = intent.getAction(); var messages= intent.getSerializableExtra("pdus"); var format=intent.getStringExtra("format"); console.log("smsmanager",messages); console.log("smsmanagerformat",format); } })

2.Do you receive SMS' while your app is running? Nothing

3.Where is the AndroidManifest file located where you register the Broadcast receiver? Is it the app/App_Resources/Android/AndroidManifest.xml? No other place would work. Yes, i declared as you mentioned

4.Where in the AndroidManifest have you inserted the block?

5.What device are you testing the receiver on? Real Android Device(Lenovo k5)

cesarvega commented 6 years ago

use it like this at the top declare this

declare let com: any;
declare let android: any

` testService() {

    com.pip3r4o.android.app.IntentService.extend("com.tns.notifications.NotificationIntentService" /* give your class a valid name as it will need to be declared in the AndroidManifest later */, {
        onHandleIntent: function (intent) {

            var action = intent.getAction();
            if ("ACTION_START" == action) {
                postNotification();
            } else if ('ACTION_STOP' == action) {
             /* get the system alarm manager and cancel all pending alarms, which will stop the service from executing periodically  */
            }
            android.support.v4.content.WakefulBroadcastReceiver.completeWakefulIntent(intent);  
        }           
     });

     function postNotification() {
        // Do something. For example, fetch fresh data from backend to create a rich notification?
        var utils = require("utils/utils");
        var context = utils.ad.getApplicationContext(); // get a reference to the application context in Android
        var builder = new android.app.Notification.Builder(context);
        builder.setContentTitle("Scheduled Notification")
            .setAutoCancel(true)
            .setColor(android.R.color.holo_purple) // optional
            .setContentText("This notification has been triggered by Notification Service")
            .setVibrate([100, 200, 100]) // optional
            .setSmallIcon(android.R.drawable.btn_star_big_on);
            // will open main NativeScript activity when the notification is pressed
        var mainIntent = new android.content.Intent(context, com.tns.NativeScriptActivity.class);
        var pendingIntent = android.app.PendingIntent.getActivity(context,
            1,
            mainIntent,
            android.app.PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);
        builder.setDeleteIntent(getDeleteIntent(context));
        var manager = context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
        manager.notify(1, builder.build());
     }

     /* only necessary for dismissing the notification from the Notifications Screen */
     function getDeleteIntent(context) {
            var intent = new android.content.Intent(context, com.tns.broadcastreceivers.NotificationEventReceiver.class);
            intent.setAction("ACTION_DELETE_NOTIFICATION");
            return android.app.PendingIntent.getBroadcast(context, 0, intent, android.app.PendingIntent.FLAG_UPDATE_CURRENT);
     }

    //BROADCASTER

     android.support.v4.content.WakefulBroadcastReceiver.extend("com.tns.broadcastreceivers.NotificationEventReceiver", {       
        onReceive: function(context, intent){
            var action = intent.getAction();
            var serviceIntent = null;
            if ("ACTION_START_NOTIFICATION_SERVICE" == action) {
                console.log("onReceive from alarm, starting notification service! thread: ");
                serviceIntent = createIntentStartNotificationService(context);
            } else if ("ACTION_DELETE_NOTIFICATION" == action) {
                console.log("onReceive delete notification action, starting notification service to handle delete");
                serviceIntent = createIntentDeleteNotification(context);
            }        
            if (serviceIntent) {
                android.support.v4.content.WakefulBroadcastReceiver.startWakefulService(context, serviceIntent);
            }
        }
     })

     var Intent = android.content.Intent;
     function createIntentStartNotificationService(context) {
        var intent = new Intent(context, com.tns.notifications.NotificationIntentService.class);
        intent.setAction("ACTION_START");
        return intent;
     }

     function createIntentDeleteNotification(context) {
        var intent = new Intent(context, com.tns.notifications.NotificationIntentService.class);
        intent.setAction("ACTION_DELETE");
        return intent;
     }

    // Set The Alarm and the intet to pass 

     function getStartPendingIntent(context) {
        var alarmIntent = new android.content.Intent(context, com.tns.broadcastreceivers.NotificationEventReceiver.class);
        alarmIntent.setAction("ACTION_START_NOTIFICATION_SERVICE");
        return android.app.PendingIntent.getBroadcast(context, 0, alarmIntent, android.app.PendingIntent.FLAG_UPDATE_CURRENT);
     }

     setupAlarm();// call setUpAlrm to beging the Intent 
     function setupAlarm() {
        var utils = require("utils/utils");
        var context = utils.ad.getApplicationContext();
        var alarmManager = context.getSystemService(android.content.Context.ALARM_SERVICE);
        var alarmIntent = getStartPendingIntent(context);
        var d = new Date();
        var currentTime = d.getTime();           

        alarmManager.setRepeating(android.app.AlarmManager.RTC_WAKEUP,          
            currentTime,
            1000  * 3, /* alarm will send the `alarmIntent` object every 3 seconds */
            alarmIntent);
     }

};

` is breaking on the com.pip3r40.android.app says is undefined :(

NickIliev commented 6 years ago

Closing due to inactivity. Please post a how-to related question in the community channels like SO, NativeScript slack or NativeScript forum

basickarl commented 4 years ago

Closing due to inactivity. Please post a how-to related question in the community channels like SO, NativeScript slack or NativeScript forum

That link to the forum doesn't work.

Regarding the initial question. I myself am in the exact situation. There is no info about this anywhere.